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

时间:2019.06.06 发布人:jazz0o

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

已解决问题

谷歌jazz0o用户在2019.06.06提交了关于“htcphp实现模拟post请求用法实例”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2023-11-30T14:52:11。希望大家能够帮助她。

详细问题描述及疑问:期待您的答案,当代劳模,所有人都应该向你学习 !

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

第1个回答

用户名:coneycl  

本文实例讲述了php实现模拟p来自ost请求的方法。分享给大家供大家参考。具体如下:
立损已女北各红class
Reque问答st{
publi死c
static
funct体深根而死汽ion
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_RETURN连美在它联古位输杆独送TRANSFER,
1);
c兵在探苦物url_setopt
($素米争轴般ch,
CURLOPT西客跑_CONNECTTIMEOUT,
$timeout弦);
curl_se留据topt($ch,
CURLOPT_h**EADER,
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'
=>
arra汽培委下未y(
'method'
=>
'POST',
'header'
=>
'Content-type:
application/x-www-form-urlen末逐解末李coded',
'content'
=>
$postdata
)
);
$context
=
stream_context_create($opts);
$result
=
file_get_contents($url,
false,
$context);
return
$result;
}
publ备风它民化祖同愿ic
static
function
post3($host,$path,$query,$others=''){//fsocket
$post="P致存倒黑族露怎罗东OST
$path
h**TTP/1.1\r\nh**ost:
$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程序设计有所帮助。