简体   繁体   English

php curl_exec返回空

[英]php curl_exec returns empty

 $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

i am confused why $exec does not return anything ,i am new to curl please help, thanks in advance 我很困惑,为什么$ exec不会返回任何东西,我是新来的卷曲请帮助,提前谢谢

curl_exec will return false when the request failed. 当请求失败时, curl_exec将返回false Adjust your function to this : 调整您的功能:

$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);

This way u'll see the curl error 这样你就会看到卷曲错误

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

You're trying to access a HTTP response code before you actually make the HTTP call. 在实际进行HTTP调用之前,您正尝试访问HTTP响应代码。 Reverse the execution as follows: 按如下方式反转执行:

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

Try: 尝试:

curl_setopt($ch, CURLOPT_TIMEOUT, 50);

Maybe the response is longer than 10. 也许响应时间超过10。

I had the same issue, I solved like this. 我遇到了同样的问题,我这样解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM