php curl_post获取不到返回结果问题!
2017-07-03 本文已影响1145人
DragonersLi
post数据到远程服务器,获取返回数据。通过代码时而有数据,时而无。print_R()返回为空,用var_dump()打印出来是string(0) "" ,用FireFox的Httprequester工具,请求结果正确!最后找到问题所在,用的封装curl_postData()函数有问题。从 http://php.net/manual/zh/function.curl-exec.php 上获取代码,结果就ok了!这个坑踩的冤!!
Paste_Image.png /**
* Send a POST requst using cURL
* @param string $url to request
* @param array $post values to send
* @param array $options for cURL
* @return string
*/
function curl_post($url, array $post = NULL, array $options = array())
{
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
CURLOPT_POSTFIELDS => http_build_query($post)
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}