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?
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:
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.
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.
Then out of curiosity I decided to turn off SELinux
temporarily by running this command:
setenforce 0
And guess what this worked. You can then properly set context by running setsebool httpd_can_network_connect on
Statuscode 0 means the connection was closed (gracefully) before any output was returned.
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.
Your command
curl -url http://www.webhostingstuff.com/addcomments/5ite.html
should have been:
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).
Hope that at least helps with the debugging.
In my case, the http code 0 was being returned because of a connection timeout. By adding
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
I was able to get rid of the error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With