php curl_exec 返回空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27121324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:11:31  来源:igfitidea点击:

php curl_exec returns empty

phpcurl

提问by user3912994

 $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 的新手,请帮忙,提前致谢

回答by DarkBee

curl_execwill return falsewhen the request failed. Adjust your function to this :

curl_execfalse在请求失败时返回。将您的功能调整为:

$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

这样你就会看到 curl 错误

回答by J Marshall Presnell

You're trying to access a HTTP response code before you actually make the HTTP call. Reverse the execution as follows:

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

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

回答by C??ng Nghiêm

The result return 0 mean that you can not connect to the server so please recheck your proxy and increase the timeout :)

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

回答by aymen zitoun

Try:

尝试:

curl_setopt($ch, CURLOPT_TIMEOUT, 50);

Maybe the response is longer than 10.

也许响应长于 10。

I had the same issue, I solved like this.

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