php带参数的post请求模拟,php模拟post请求方法总结

php 模拟 post 请求方法主要有两种办法,通过 fsocket 和通过 curl。

下面就简单的举两个案例说明一下 php 如何使用这两种方法模拟 post 请求。

(1)php 通过 fsocket 模拟 post 提交请求

function sock_post($url,$query){

$info=parse_url($url);

$fp=fsockopen($info["host"],80,$errno,$errstr,3);

$head="POST ".$info['path']." HTTP/1.0\r\n";

$head.="Host: ".$info['host']."\r\n";

$head.="Referer: http://".$info['host'].$info['path']."\r\n";

$head.="Content-type: application/x-www-form-urlencoded\r\n";

$head.="Content-Length: ".strlen(trim($query))."\r\n";

$head.="\r\n";

$head.=trim($query);

$write=fputs($fp,$head);

while(!feof($fp)){

$line=fgets($fp);

echo $line."

";

}

}

使用方法如下(注意$url这个参数必须是域名,不可以是localhost这种形式的url):

$purl="http://www.scutephp.com/post.php";echo "以下是POST方式的响应内容:

";

sock_post($purl,"name=php程序员教程网&url=http://www.scutephp.com/");

(2)php 通过 curl 模拟 post 提交请求

$url='http://www.scutephp.com/post.php';

$fields=array(

'lname'=>'justcoding',

'fname'=>'phplover',

'title'=>'myapi',

'email'=>'1290026290@qq.com',

'phone'=>'188888888888'

);

$ch=curl_init();

curl_setopt($ch,CURLOPT_URL,$url);

curl_setopt($ch,CURLOPT_POST,count($fields));

curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);

ob_start();

curl_exec($ch);

$result=ob_get_contents();

ob_end_clean();

echo $result;

curl_close($ch); 本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉 本文系统来源:php中文网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值