php return 0,PHP cURL HTTP CODE return 0

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I dont understand when I echo $httpCode I always get 0, I was expecting 404 when I change $html_brand into a broken url. Is there anything that I miss or do not know of? Thanks. //check if url exist $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $html_brand); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($httpCode == 404) { echo "The Web Page Cannot Be Found"; return; } curl_close($ch);

回答1:

If you connect with the server, then you can get a return code from it, otherwise it will fail and you get a 0. So if you try to connect to "www.google.com/lksdfk" you will get a return code of 400, if you go directly to google.com, you will get 302 (and then 200 if you forward to the next page... well I do because it forwards to google.com.br, so you might not get that), and if you go to "googlecom" you will get a 0 (host no found), so with the last one, there is nobody to send a code back.

Tested using the code below. $html_brand, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => "", CURLOPT_AUTOREFERER => true, CURLOPT_CONNECTTIMEOUT => 120, CURLOPT_TIMEOUT => 120, CURLOPT_MAXREDIRS => 10, ); curl_setopt_array( $ch, $options ); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ( $httpCode != 200 ){ echo "Return code is {$httpCode} \n" .curl_error($ch); } else { echo "".htmlspecialchars($response).""; } curl_close($ch);

回答2:

Try this after curl_exec to see what's the problem: print curl_error($ch);

If it's print something like 'malformed' then check your URL format.

回答3:

What is the exact contents you are passing into $html_brand?

If it is has an invalid URL syntax, you will very likely get the HTTP code 0.

回答4:

Like said here and below, a failed request (i.e. the server is not found) returns false, no HTTP status code, since a reply has never been received.

回答5:

check the curl_error after the curl_getinfo to find out the hidden errors. if(curl_errno($ch)){ echo 'Curl error: ' . curl_error($ch); }

回答6:

I had same problem and in my case this was because curl_exec function is disabled in php.ini. Check for logs: PHP Warning: curl_exec() has been disabled for security reasons in /var/www/***/html/test.php on line 18

Solution is remove curl_exec from disabled functions in php.ini on server configuration file.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值