为什么我从curl获得HTTP_CODE 0?

Sau*_*ius 12 php curl http

我一直在使用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"
                    );
Run Code Online (Sandbox Code Playgroud)

我有一个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);    

}
Run Code Online (Sandbox Code Playgroud)

我也试过从不同的服务器通过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 ~]$ 

我怎么解决这个问题?

Tas*_*iwa 9

你的命令

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

本来应该:

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

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

希望至少有助于调试.


Hal*_*yon 8

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

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


Dmy*_*kin 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
Run Code Online (Sandbox Code Playgroud)

我使用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();
Run Code Online (Sandbox Code Playgroud)