php 为什么我从 curl 得到 HTTP_CODE 0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6887104/
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
Why am I getting HTTP_CODE 0 from curl?
提问by Saulius Antanavicius
I have been using curl with PHP for a while. Today I've been trying to fetch http://www.webhostingstuff.com/category/Best-Hosting.htmland I keep getting http code 0, which is new to me.
我在 PHP 中使用 curl 已经有一段时间了。今天我一直在尝试获取http://www.webhostingstuff.com/category/Best-Hosting.html并且我不断收到 http 代码 0,这对我来说是新的。
I set the headers
我设置了标题
$s->headers = array(
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: en-gb,en;q=0.5",
"Accept-Encoding: gzip, deflate",
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Keep-Alive: 115",
"Connection: keep-alive",
"Referer: https://google.com"
);
and I have a cookie file (which has nothing in it when the script finishes loading)
我有一个 cookie 文件(当脚本完成加载时,里面什么都没有)
Here's the curl function
这是 curl 函数
function fetch($url, $username='', $data='', $proxy=''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
if(isset($proxy)) {
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'proxyadmin:parola');
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT,true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
if(!empty($username)) {
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie/{$username}.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie/{$username}.txt");
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if (is_array($data) && count($data)>0) {
curl_setopt($ch, CURLOPT_POST, true);
$params = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
if (is_array($this->headers) && count($this->headers)>0){
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
}
$this->result = curl_exec($ch);
$curl_info = curl_getinfo($ch);
$header_size = $curl_info["header_size"];
$this->headers = substr($this->result, 0, $header_size);
$this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->error = curl_error($ch);
curl_close($ch);
}
I've also tried to execute via SSH from a different server(in case it's IP blocked)
我还尝试从不同的服务器通过 SSH 执行(以防 IP 被阻止)
[brian@ip-184-168-22-244 ~]$ curl -url http://www.webhostingstuff.com/addcomments/5ite.html Enter host password for user 'rl': curl: (7) couldn't connect to host [brian@ip-184-168-22-244 ~]$
How might I resolve this?
我该如何解决这个问题?
回答by Tash Pemhiwa
Your command
你的命令
curl -url http://www.webhostingstuff.com/addcomments/5ite.html
curl -url http://www.webhostingstuff.com/addcomments/5ite.html
should have been:
本来应该:
curl --url http://www.webhostingstuff.com/addcomments/5ite.html
curl --url http://www.webhostingstuff.com/addcomments/5ite.html
cURL thinks you are specifying the -u option, which is used to specify a username, hence the error message you got. You need to specify --url (two dashes).
cURL 认为您正在指定 -u 选项,该选项用于指定用户名,因此您收到错误消息。您需要指定 --url (两个破折号)。
Hope that at least helps with the debugging.
希望至少有助于调试。
回答by Halcyon
Statuscode 0 means the connection was closed (gracefully) before any output was returned.
状态码 0 表示在返回任何输出之前连接已关闭(正常)。
I guess I'd start by figuring out whether you can connect to the machine at all. If you have access to the remote machine it will likely help debugging.
我想我会首先弄清楚您是否可以连接到机器。如果您有权访问远程机器,它可能有助于调试。
回答by toneplex
In my case, the http code 0 was being returned because of a connection timeout. By adding
就我而言,由于连接超时,返回了 http 代码 0。通过添加
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
I was able to get rid of the error
我能够摆脱错误
回答by Naty
0 code means curl can't find the server you were looking for. "yahoo.com/whatever" will return 404, while "yahoo.comwhatever" will return 0.
0 代码表示 curl 找不到您要查找的服务器。"yahoo.com/whatever" 将返回 404,而 "yahoo.comwhatever" 将返回 0。
回答by vishwakarma09
Yesterday I faced similar problem. I spent 2 hours on this issue. I was on RHEL system. The curl code had below block for authentication:
昨天我遇到了类似的问题。我在这个问题上花了 2 个小时。我在RHEL系统上。curl 代码具有以下用于身份验证的块:
if($httpCode == 200) {
$_SESSION["username"] = $username;
$_SESSION["password"] = $password;
return array(true, "Login successful. Please wait while you are redirected to home page.");
}else if($httpCode == 401){
return array(false, "Login failure. Incorrect username / password");
}
This code was being used for authentication. In lab environment it returned 200, but on production it returned 0.
此代码正用于身份验证。在实验室环境中它返回 200,但在生产环境中它返回 0。
Then I made a similar script (which used curl), and run if from command line like php test3.php
. This run resulted in 200
status code.
然后我制作了一个类似的脚本(使用 curl),并从命令行运行 if 像php test3.php
. 此运行导致200
状态代码。
Then out of curiosity I decided to turn off SELinux
temporarily by running this command:
然后出于好奇,我决定SELinux
通过运行以下命令暂时关闭:
setenforce 0
And guess what this worked. You can then properly set context by running setsebool httpd_can_network_connect on
猜猜这有什么用。然后,您可以通过运行正确设置上下文setsebool httpd_can_network_connect on
回答by Dmytro Zavalkin
Maybe it was some internal server error? Right now it works:
也许这是一些内部服务器错误?现在它有效:
> GET /category/Best-Hosting.html HTTP/1.1
> User-Agent: HTTP_Request2/0.5.2 (http://pear.php.net/package/http_request2) PHP/5.2.12
> Host: www.webhostingstuff.com
> Accept: */*
> Accept-Encoding: deflate, gzip
>
< HTTP/1.1 200 OK
< date: Sun, 31 Jul 2011 10:57:43 GMT
< server: Apache
< last-modified: Sun, 31 Jul 2011 10:55:00 GMT
< content-encoding: gzip
< vary: Accept-Encoding
< transfer-encoding: chunked
< content-type: text/html
I use HTTP_Request2pear package as curl wrapper, the code:
我使用HTTP_Request2pear 包作为 curl 包装器,代码:
$url = 'http://www.webhostingstuff.com/category/Best-Hosting.html';
$request = new HTTP_Request2 (
$url,
HTTP_Request2::METHOD_GET,
array (
'adapter' => new HTTP_Request2_Adapter_Curl(),
'ssl_verify_peer' => false,
)
);
$request->attach(new HTTP_Request2_Observer_Log('log.txt'));
$result = $request->send();