PHP CURL POST
最近公司运营平台因为要接通另外一个服务端的接口,要求模拟HTTP POST 提交数据。很久没有写过CURL了,特意上官网查了下文档以及复习下HTTP POST参数所代表的含义,根据HTTP POST参数所要求模拟的一个POST请求,发放出来给大家共同交流学习。代码的注释是
/*
* [Content-Length] => 48
* [Content-Type] => application/x-www-form-urlencoded
* [Accept] => *\/*
* [User-Agent] => Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15
* [Host] => localhost )
*/
public static function doCurlPostRequest($url,$post_data){
$postData = http_build_query($post_data);
$curl = curl_init();
//SET POST REQUEST LINE
curl_setopt($curl, CURLOPT_URL, $url);//SET URL
curl_setopt($curl, CURLOPT_POST, true);//SET POST
//SET HEADER
curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');//CLINET BROW
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //STOP VVERIFYING CERTIFICATE
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //NOT OUTPUT
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));//HEARD MIME
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);//FORWARD IS TRUE
//SET POST DATA
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
$output = curl_exec($curl);
if ($output == NULL) {
return 0;
}
curl_close($curl);
return $output;
}
/*
* OUTPUT HEADERS
*/
public function getallheaders()
{
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。