当前位置:首页 » 编程语言 » php请求post

php请求post

发布时间: 2023-01-10 14:12:30

php实现模拟post请求用法实例

本文实例讲述了php实现模拟post请求的方法。分享给大家供大家参考。具体如下:
class
Request{
public
static
function
post($url,
$post_data
=
'',
$timeout
=
5){//curl
$ch
=
curl_init();
curl_setopt
($ch,
CURLOPT_URL,
$url);
curl_setopt
($ch,
CURLOPT_POST,
1);
if($post_data
!=
''){
curl_setopt($ch,
CURLOPT_POSTFIELDS,
$post_data);
}
curl_setopt
($ch,
CURLOPT_RETURNTRANSFER,
1);
curl_setopt
($ch,
CURLOPT_CONNECTTIMEOUT,
$timeout);
curl_setopt($ch,
CURLOPT_HEADER,
false);
$file_contents
=
curl_exec($ch);
curl_close($ch);
return
$file_contents;
}
public
static
function
post2($url,
$data=array()){//file_get_content
$postdata
=
http_build_query(
$data
);
$opts
=
array('http'
=>
array(
'method'
=>
'POST',
'header'
=>
'Content-type:
application/x-www-form-urlencoded',
'content'
=>
$postdata
)
);
$context
=
stream_context_create($opts);
$result
=
file_get_contents($url,
false,
$context);
return
$result;
}
public
static
function
post3($host,$path,$query,$others=''){//fsocket
$post="POST
$path
HTTP/1.1\r\nHost:
$host\r\n";
$post.="Content-type:
application/x-www-form-";
$post.="urlencoded\r\n${others}";
$post.="User-Agent:
Mozilla
4.0\r\nContent-length:
";
$post.=strlen($query)."\r\nConnection:
close\r\n\r\n$query";
$h=fsockopen($host,80);
fwrite($h,$post);
for($a=0,$r='';!$a;){
$b=fread($h,8192);
$r.=$b;
$a=(($b=='')?1:0);
}
fclose($h);
return
$r;
}
}
$url='http://******/con/Inter.php';
$data=Request::post($url,array('api'=>'tag_list'));
$data2=Request::post2($url,array('api'=>'tag_list'));
echo
$data;
希望本文所述对大家的php程序设计有所帮助。

② PHP 模拟HTTP发送POST请求

用php模拟登陆主要分为三部分1. post数据。2.根据返回的http头,从中截出cookie段。3.伪造http头发送请求。 我这里以用php抓取163相册的需要密码才能访问的目录为例。<?phpfunction posttohost($url, $data) //post数据if (!isset($url['query'])) $encoded = "";foreach ($data as $k=>$v) $fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);if (!$fp) return "Failed to open socket to $url[host]";fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? "?" : "", $url['query']));fputs($fp, "Host: $url[host]\n");fputs($fp, "Content-type: application/x-www-form-urlencoded\n");fputs($fp, "Content-length: " . strlen($encoded) . "\n");fputs($fp, "Connection: close\n\n");fputs($fp, "$encoded\n");$line = fgets($fp,1024);if (!eregi("^HTTP/1\.. 200", $line)) return;$results = ""; $inheader = 1;while(!feof($fp)) elseif ($inheader) }fclose($fp);return $results;} 答案补充 function getjs($juser,$jaid,$jcookie) //伪造http头 答案补充 else fclose( $socket ); return $ret;}}$iurl=' http://photo.163.com/photos/'.$iuser.'/'.$aid.'/';$idata=array ('pass'=>$pass,'checking'=>'1'); //通过winsock抓包发现,输入访问密码,会向163相册发送 pass=密码&checking=1$mystr=posttohost($iurl,$idata);$pattern='/HALFORDER=(.*?);/';preg_match($pattern,$mystr,$out);$str=getjs($iuser,$aid,$out[1]);echo $str;?>

③ 关于PHP中POST传递参数问题

将数据转换成 json 格式的字符串, 并通过 CURL 的 POST 的形式传递参数给服务端, 但是在服务端无法用 $_POST 获取到数据。后台用 $_POST 获取到的信息为空, 但是可以通过 $post = file_get_contents("php://input") 获取到请求的相关信息。

Coentent-Type 的值为 application/x-www-data-urlencode 和 multipart/form-data 时, php才会将http请求数据包中的数据填进 $_POST 。

如果 POST 的原始数据是一维数组或&拼接的标准格式的键值对字符串,那么可以用 $_POST 来获取。
如果要通过 file_get_contents 获取,这种情况下可以发送 json 字符串,用 json_encode 编码转换一下,或者使用 http_build_query 。

1、 区别 PHP 的 $_POST、$HTTP_RAW_POST_DATA 和 php://input
2、 accept 和 content-Type区别
3、 Http Header里的Content-Type

④ php怎样判断当前请求是post还是get

具体的判断方法如下:

⑤ 怎么用PHP发送POST请求

PHP发送POST请求的三种方式

classRequest{

publicstaticfunctionpost($url,$post_data='',$timeout=5){//curl

$ch=curl_init();

curl_setopt($ch,CURLOPT_URL,$url);

curl_setopt($ch,CURLOPT_POST,1);

if($post_data!=''){

curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);

}

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);

curl_setopt($ch,CURLOPT_HEADER,false);

$file_contents=curl_exec($ch);

curl_close($ch);

return$file_contents;

}


publicstaticfunctionpost2($url,$data){//file_get_content$postdata=http_build_query(

$data

);$opts=array('http'=>

array(

'method'=>'POST',

'header'=>'Content-type:application/x-www-form-urlencoded',

'content'=>$postdata

)

);$context=stream_context_create($opts);


$result=file_get_contents($url,false,$context);

return$result;


}


publicstaticfunctionpost3($host,$path,$query,$others=''){//fsocket


$post="POST$pathHTTP/1.1 Host:$host ";

$post.="Content-type:application/x-www-form-";

$post.="urlencoded ${others}";

$post.="User-Agent:Mozilla4.0 Content-length:";

$post.=strlen($query)." Connection:close $query";

$h=fsockopen($host,80);

fwrite($h,$post);

for($a=0,$r='';!$a;){

$b=fread($h,8192);

$r.=$b;

$a=(($b=='')?1:0);

}

fclose($h);

return$r;

}
}

http://www.oschina.net/code/snippet_729516_33065

⑥ PHP中怎样发送post请求并获取网页

$post='POST数据';
//初始化
$curl=curl_init('URL');
$header=array();
$header[]='User-Agent:Mozilla/5.0(WindowsNT6.1)AppleWebKit/537.36(KHTML,likeGecko)Chrome/42.0.2311.90Safari/537.36';
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
//不输出header头信息
curl_setopt($curl,CURLOPT_HEADER,0);
//保存到字符串而不是输出
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
//post数据
curl_setopt($curl,CURLOPT_POST,1);
//请求数据
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
//是否抓取跳转后的页面
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
$response=curl_exec($curl);
curl_close($curl);
echo$response;

⑦ PHP。POST请求的问题

最简单的话就是使用session保存,其次可以把数据存储数据库里,或者文件里,然后在register.php里查询。
原生session使用方法
session_start();
//赋值
$_SESSION["Session名称"]=变量或字符串信息;
//使用
$_SESSION["Session名称"];

⑧ php 怎么POST获取数据

方法1、最常见的方法是:$_post['fieldname'];
说明:只能接收content-type:
application/x-www-form-urlencoded提交的数据
解释:也就是表单post过来的数据
方法2、file_get_contents("php://input");
说明:
允许读取
post
的原始数据。

$http_raw_post_data
比起来,它给内存带来的压力较小,并且不需要任何特殊的
php.ini
设置。
php://input
不能用于
enctype="multipart/form-data"。
解释:
对于未指定
content-type
的post数据,则可以使用file_get_contents(“php://input”);来获取原始数据。
事实上,用php接收post的任何数据都可以使用本方法。而不用考虑content-type,包括二进制文件流也可以。
所以用方法二是最保险的方法

⑨ php发送get,post请求的几种方法

POST方法比较适合用于发送一个保密的(比如信用卡号)或者比较大量的数据到服务器,但速度慢。而Get方法会将所要传输的数据附在网址后面,然后一起送达服务器,因此传送的数据量就会受到限制且不安全,但是执行效率却比 Post方法好。具体来说如下:
1、Get将表单中数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接,而各个变量之间使用“&”连接;Post是将表单中的数据放在form的数据体中,按照变量和值相对应的方式,传递到action所指向URL。
2、Get是不安全的,因为在传输过程,数据被放在请求的URL中,而如今现有的很多服务器、代理服务器或者用户代理都会将请求URL记录到日志文件中,然后放在某个地方,这样就可能会有一些隐私的信息被第三方看到。另外,用户也可以在浏览器上直接看到提交的数据,一些系统内部消息将会一同显示在用户面前。Post的所有操作对用户来说都是不可见的。
3、Get传输的数据量小,这主要是因为受URL长度限制;而Post可以传输大量的数据,所以在上传文件只能使用Post(当然还有一个原因,将在后面的提到)。
4、Get限制Form表单的数据集的值必须为ASCII字符;而Post支持整个ISO10646字符集。
5、Get是Form的默认方法。

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:585
制作脚本网站 发布:2025-10-20 08:17:34 浏览:881
python中的init方法 发布:2025-10-20 08:17:33 浏览:574
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:761
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:677
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1005
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:250
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:108
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:799
python股票数据获取 发布:2025-10-20 07:39:44 浏览:705