php发送post请求的三种方法

时间:2016.04.19 发布人:杯具鹅莫弃求虐

php发送post请求的三种方法

已解决问题

谷歌杯具鹅莫弃求虐用户在2016.04.19提交了关于“元通古镇php发送post请求的三种方法”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2025-02-13T20:32:31。希望大家能够帮助她。

详细问题描述及疑问:期待您的答案,不知道说什么,送你一朵小红花吧 !

希望以下的回答,能够帮助你。

第1个回答

用户名:vxjbfe  

复制代码代码如下:
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_h**EADER,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$pathh**TTP/1.1\r\nh**ost:$host\r\n";

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

$post.="urlencoded\r\n${others}";

$post.="User-Agent:Mozilla4.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;

}
}