PHP cURL HTTP CODE 返回 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10227879/
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
PHP cURL HTTP CODE return 0
提问by Ardeus
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.
我不明白当我回显 $httpCode 时我总是得到 0,当我将 $html_brand 更改为损坏的 url 时,我期待 404。有什么我想念或不知道的吗?谢谢。
//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);
采纳答案by craniumonempty
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.
如果你连接到服务器,那么你可以从它得到一个返回码,否则它会失败并且你得到一个 0。所以如果你尝试连接到“www.google.com/lksdfk”你会得到一个返回码400,如果你直接去 google.com,你会得到 302(如果你转发到下一页,然后是 200......我这样做是因为它转发到 google.com.br,所以你可能不会得到那个),如果你去“googlecom”,你会得到一个 0(找不到主机),所以最后一个,没有人可以发回代码。
Tested using the code below.
使用以下代码进行测试。
<?php
$html_brand = "www.google.com";
$ch = curl_init();
$options = array(
CURLOPT_URL => $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 "<pre>".htmlspecialchars($response)."</pre>";
}
curl_close($ch);
回答by Hereblur
Try this after curl_exec to see what's the problem:
在 curl_exec 之后试试这个,看看有什么问题:
print curl_error($ch);
If it's print something like 'malformed' then check your URL format.
如果它打印出类似“格式错误”的内容,请检查您的 URL 格式。
回答by Daniel
check the curl_errorafter the curl_getinfo to find out the hidden errors.
在curl_getinfo之后检查curl_error 以找出隐藏的错误。
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
回答by Gondy
I had same problem and in my case this was because curl_exec function is disabled in php.ini. Check for logs:
我有同样的问题,在我的情况下,这是因为 curl_exec 函数在 php.ini 中被禁用。检查日志:
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.
解决方案是从服务器配置文件上的 php.ini 中的禁用功能中删除 curl_exec。
回答by CodeCaster
Like said hereand below, a failed request (i.e. the server is not found) returns false, no HTTP status code, since a reply has never been received.
就像这里和下面所说的那样,失败的请求(即未找到服务器)返回 false,没有 HTTP 状态代码,因为从未收到回复。
Call curl_error().
打电话curl_error()。
回答by psynnott
What is the exact contents you are passing into $html_brand?
您传递给 $html_brand 的确切内容是什么?
If it is has an invalid URL syntax, you will very likely get the HTTP code 0.
如果它具有无效的 URL 语法,您很可能会得到 HTTP 代码 0。
回答by Alejandro Hdez. Angeles
Another reason for PHP to return http code 0 is timeout. In my case, I had the following configuration:
PHP 返回 http 代码 0 的另一个原因是超时。就我而言,我有以下配置:
curl_setopt($http, CURLOPT_TIMEOUT_MS,500);
curl_setopt($http, CURLOPT_TIMEOUT_MS,500);
It turned out that the request to the endpoint I was pointing to always took more than 500 ms, always timing out and always returning http code 0.
结果是,对我指向的端点的请求总是超过 500 毫秒,总是超时并且总是返回 http 代码 0。
If you remove this setting (CURLOPT_TIMEOUT_MS) or put a higher value (in my case 5000), you'll get the actual http code, in my case a 200 (as expected).
如果您删除此设置 (CURLOPT_TIMEOUT_MS) 或设置更高的值(在我的情况下为 5000),您将获得实际的 http 代码,在我的情况下为 200(如预期)。

