在做接口,post传递方式,数据以字符串形式传输,返回数据用JSON封装。
然后就开始各种测试啊。
分享最终的方法:
定义抓取函数:
function http_post_data($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($return_code, $return_content);
}然后是方法:
$url = "路径"; $data = array(); //数组 $data = json_encode($data); //转化为字符串 list($return_code, $return_content) = http_post_data($url, $data); $return_content = json_decode($return_content,1); var_dump($return_content); //输出返回结果。
