php curl_exec 返回空

新手上路,请多包涵
 $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 不返回任何东西,我是 curl 的新手,请帮忙,在此先感谢

原文由 user3912994 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

当请求失败时, 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);

这样你会看到卷曲错误

原文由 DarkBee 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

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

原文由 J Marshall Presnell 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏