传递post值需要进行post模拟,可以有三种方式进行POST模拟:
curl
socket
file_get_contents
这三种方式的原理类似,都是模拟HTTP访问,并且将POST数据提交到服务器页面上。
curl进行post提交示例代码如下:
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//指定post网页地址
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且返回输出
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch);
其中$curlPost为post字段,可以是字符串或者数组,字符串为 arg1=a&arg2=b&arg3=c 这样,数组类似 array('arg1' => 'a', 'arg2' => 'b', 'arg3' => 'c') 这样。