php curl_exec返回空

时间:2014-11-25 07:53:44

标签: php curl

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_PROXY, $proxy); // $proxy is ip of proxy server
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
 $httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
 echo '<br> curl'.$ch; //this line outputs resource id#5
 $exec = stripslashes(curl_exec($ch)); 
 echo '<br> exec'.curl_exec($ch); //this results blank

我很困惑为什么$ exec不返回任何东西,我是新来的卷曲请帮助,提前谢谢

4 个答案:

答案 0 :(得分:21)

请求失败时,

curl_exec将返回false。调整您的功能:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy); // $proxy is ip of proxy server
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
$response = curl_exec($ch);

if ($response === false) 
    $response = curl_error($ch);

echo stripslashes($response);

curl_close($ch);

这样你就会看到卷曲错误

答案 1 :(得分:1)

结果返回0意味着您无法连接到服务器,因此请重新检查您的代理并增加超时:)

答案 2 :(得分:1)

在实际进行HTTP调用之前,您尝试访问HTTP响应代码。反向执行,如下所示:

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time

答案 3 :(得分:0)

尝试:

curl_setopt($ch, CURLOPT_TIMEOUT, 50);

也许响应时间超过10。

我有同样的问题,我这样解决了。