为什么我从curl获得HTTP_CODE 0?

时间:2011-07-31 02:06:48

标签: php http curl

我一直在使用curl和PHP。今天我一直在尝试获取http://www.webhostingstuff.com/category/Best-Hosting.html,我不断获得http代码0,这对我来说是新的。

我设置了标题

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

我有一个cookie文件(当脚本加载时没有任何内容)

这是卷曲功能

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

}

我还试图通过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 ~]$ 

我该如何解决这个问题?

6 个答案:

答案 0 :(得分:9)

你的命令

  

curl -url http://www.webhostingstuff.com/addcomments/5ite.html

应该是:

  

curl --ur http://www.webhostingstuff.com/addcomments/5ite.html

cURL认为您正在指定-u选项,该选项用于指定用户名,因此您会收到错误消息。您需要指定--url(两个破折号)。

希望至少有助于调试。

答案 1 :(得分:8)

状态码0表示在返回任何输出之前关闭(正常)连接。

我想我首先想弄清楚你是否可以连接到机器上。如果您可以访问远程计算机,则可能有助于调试。

答案 2 :(得分:3)

就我而言,由于连接超时,正在返回http代码0。添加

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);

我能够摆脱错误

答案 3 :(得分:0)

也许这是一些内部服务器错误?现在它起作用了:

> 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

我使用HTTP_Request2 pear包作为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();

答案 4 :(得分:0)

0代码表示curl找不到您要查找的服务器。 “yahoo.com/whatever”将返回404,而“yahoo.comwhatever”将返回0。

答案 5 :(得分:0)

昨天我遇到了类似的问题。我在这个问题上花了2个小时。我在RHEL系统上。卷曲代码在块下面进行身份验证:

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

此代码用于身份验证。在实验室环境中它返回200,但在生产时它返回0。

然后我制作了一个类似的脚本(使用curl),然后从php test3.php这样的命令行运行。此次运行产生了200状态代码。

然后出于好奇,我决定通过运行此命令暂时关闭SELinux

setenforce 0

猜猜这是有效的。然后,您可以通过运行setsebool httpd_can_network_connect on

来正确设置上下文