Header-Ad

PHP,Joomla,WordPress & Linux based server management, error resolutions and fixing problems.

Sunday, November 13, 2016

PHP cURL HTTP CODE return 0 - How to Debug? [Solved]

PHP cURL HTTP CODE return 0



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 "geeks-wiki.blogspot.com/lksdfk" you will get a return code of 400, if you go directly to geeks-wiki.blogspot.com, you will get 302 (and then 200 if you forward to the next page... 
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);

The more easier approach is to check curl_error() as what it returns and then check the cURL Manual at PHP's website! Source : https://www.stackflair.com/curl-error0-in-php/

No comments:

Post a Comment

** Comments are reviewed, but not delayed posted **