121

I dont understand when I echo $httpCode I always get 0, I was expecting 404 when I change $html_brand into a broken url. Is there anything that I miss or do not know of? Thanks.

 //check if url exist
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $html_brand);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch); 
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($httpCode == 404) {
    echo "The Web Page Cannot Be Found";
    return;
}
curl_close($ch);
Ardeus
  • 1,921
  • 3
  • 17
  • 26

12 Answers12

115

If you connect with the server, then you can get a return code from it, otherwise it will fail and you get a 0. So if you try to connect to "www.google.com/lksdfk" you will get a return code of 400, if you go directly to google.com, you will get 302 (and then 200 if you forward to the next page... well I do because it forwards to google.com.br, so you might not get that), and if you go to "googlecom" you will get a 0 (host no found), so with the last one, there is nobody to send a code back.

Tested using the code below.

<?php

$html_brand = "www.google.com";
$ch = curl_init();

$options = array(
    CURLOPT_URL            => $html_brand,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER         => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_ENCODING       => "",
    CURLOPT_AUTOREFERER    => true,
    CURLOPT_CONNECTTIMEOUT => 120,
    CURLOPT_TIMEOUT        => 120,
    CURLOPT_MAXREDIRS      => 10,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch); 
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ( $httpCode != 200 ){
    echo "Return code is {$httpCode} \n"
        .curl_error($ch);
} else {
    echo "<pre>".htmlspecialchars($response)."</pre>";
}

curl_close($ch);
craniumonempty
  • 3,525
  • 2
  • 20
  • 18
  • 1
    Also thx for refencing followlocation too, sometimes i get 301 which is a redirecting issue. – Ardeus Apr 20 '12 at 07:16
  • Its weird even though $response has html content, but the $httpcode always return 0, plus the curl_error is null . what could possible went wrong. I only include returntransfer, header (false), followlocation in the setting only. – Ardeus May 02 '12 at 12:08
  • Just to add, if there are any whitespaces before or after the URL, it will also throw the http_code = 0 – Raja Amer Khan Apr 15 '19 at 14:30
  • 1
    [Someone copied your answer and only changed the URL](http://phpcorelab.com/why-php-curl-http-code-return-0-zero/) – SteeveDroz Aug 16 '19 at 08:28
107

Try this after curl_exec to see what's the problem:

print curl_error($ch);

If it's print something like 'malformed' then check your URL format.

Hereblur
  • 2,084
  • 1
  • 19
  • 22
10

check the curl_error after the curl_getinfo to find out the hidden errors.

if(curl_errno($ch)){   
    echo 'Curl error: ' . curl_error($ch);
}
Daniel
  • 387
  • 2
  • 4
8

Try this:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
Meldo
  • 146
  • 1
  • 6
  • 6
    When answering an older question with an accepted answer it is important to point out what new aspect of the question your answer addresses. Also code only answers can generally be improved by adding some explanation of how and why they work. Also when suggesting that someone disable security it is VERY important to explain why and what the risks are. – Jason Aller Jun 30 '20 at 20:46
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Jul 01 '20 at 16:14
  • 5
    **This disables HTTPS verification. Do not use this unless absolutely nothing else works** - and if it does, make it a priority to identify exactly why, and what you might be able to fix elsewhere in the stack. – i336_ Jul 02 '21 at 17:35
4

Like said here and below, a failed request (i.e. the server is not found) returns false, no HTTP status code, since a reply has never been received.

Call curl_error().

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
3

I had same problem and in my case this was because curl_exec function is disabled in php.ini. Check for logs:

PHP Warning:  curl_exec() has been disabled for security reasons in /var/www/***/html/test.php on line 18

Solution is remove curl_exec from disabled functions in php.ini on server configuration file.

Gondy
  • 4,925
  • 4
  • 40
  • 46
2

What is the exact contents you are passing into $html_brand?

If it is has an invalid URL syntax, you will very likely get the HTTP code 0.

psx
  • 4,040
  • 6
  • 30
  • 59
2

Another reason for PHP to return http code 0 is timeout. In my case, I had the following configuration:

curl_setopt($http, CURLOPT_TIMEOUT_MS,500);

It turned out that the request to the endpoint I was pointing to always took more than 500 ms, always timing out and always returning http code 0.

If you remove this setting (CURLOPT_TIMEOUT_MS) or put a higher value (in my case 5000), you'll get the actual http code, in my case a 200 (as expected).

See https://www.php.net/manual/en/function.curl-setopt.php

2

If you're using selinux it might be because of security restrictions. Try setting this as root:

# setsebool -P httpd_can_network_connect on
Samo
  • 2,093
  • 1
  • 17
  • 18
2

Also check the order in which the commands are executed.

First curl_exec and then curl_getinfo.

$response = curl_exec($ch); 
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Maxim Mandrik
  • 377
  • 1
  • 5
  • 11
1

For me, issue wat the URL, untill I encoded url parameter values, I got http code 0.

Dharman
  • 30,962
  • 25
  • 85
  • 135
temo
  • 612
  • 1
  • 9
  • 25
1

In my case, I get CURLINFO_RESPONSE_CODE 0 (zero) whenever I have white space (i.e. URL is not encoded) in the URL. And literally anywhere in the URL (address part, query part, any). So, this is not good:

https://my.do main.com //(this is never good)

or

https://my.domain.com?date=2022-12-29 13:55:10&type=apparel //you can see how date formatting breaks the query for cURL

etc. Obviously, a requested link or parts of it must be assembled properly and parameter values of it in the url query must be percent encoded if a value can, by the definition, contain an empty string. I know this is not some big issue, but I had such a case, and I don't see it in the answers here.

Dbertovi
  • 51
  • 3