Why am I getting HTTP_CODE 0 from curl?

Viewed 30905

I have been using curl with PHP for a while. Today I've been trying to fetch http://www.webhostingstuff.com/category/Best-Hosting.html and I keep getting http code 0, which is new to me.

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)

Here's the curl function

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)

[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?

6 Answers
Related