php curl_exec返回空值

ttisahbt  于 2022-12-17  发布在  PHP
关注(0)|答案(4)|浏览(258)
$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请帮助,提前感谢

mf98qq94

mf98qq941#

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

这样你会看到 curl 错误

fnx2tebb

fnx2tebb2#

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

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

tmb3ates3#

试试看:

curl_setopt($ch, CURLOPT_TIMEOUT, 50);

也许响应比10长。
我也遇到过同样的问题,我这样解决了。

gudnpqoy

gudnpqoy4#

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

相关问题