Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP cURL HTTP CODE return 0

Tags:

php

curl

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); 
like image 923
Ardeus Avatar asked Apr 19 '12 12:04

Ardeus


People also ask

What does PHP CURL return?

Functions of cURL in PHP curl_error — It will return the string which represents the error for the particular current session. curl_exec — After a cURL session has been created and all of the session's options have been set, the function should be named. Its sole aim is to run a predefined CURL session (given by ch).

How do I get my HTTP CURL code?

curl -v will give you the header and body. Use --output to write the content to a (temporary) file, and --write-out to output things like HTTP status.

How do I print a CURL response?

By default, curl doesn't print the response headers. It only prints the response body. To print the response headers, too, use the -i command line argument.

How check CURL URL in PHP?

PHP cURL status code php $ch = curl_init('http://webcode.me'); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); echo $status; curl_close($ch);


2 Answers

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); 
like image 170
craniumonempty Avatar answered Oct 04 '22 00:10

craniumonempty


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.

like image 25
Hereblur Avatar answered Oct 03 '22 23:10

Hereblur