PHP CURL POST
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; }
相关推荐
CyborgLin 2020-08-15
83911535 2020-11-13
曾是土木人 2020-10-31
yegen00 2020-10-21
soralaro 2020-10-11
katanaFlower 2020-09-18
wytzsjzly 2020-08-17
88407710 2020-08-17
ChinaJoeEE 2020-08-16
Blueberry 2020-08-15
PinkBean 2020-08-11
katanaFlower 2020-08-03
hunningtu 2020-07-30
阿债的方寸天地 2020-06-28
pingyan 2020-06-25
wytzsjzly 2020-06-25
阳光岛主 2020-06-25
阿债的方寸天地 2020-06-16