0

I am trying to check if a page is down, 404, 500, 403 etc..

I am using this

function is_404($url) {
    $handle = curl_init($url);
    curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);

    /* Get the HTML or whatever is linked in $url. */
    $response = curl_exec($handle);

    /* Check for 404 (file not found). */
    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    curl_close($handle);

    /* If the document has loaded successfully without any redirection or error */
    if ($httpCode >= 200 && $httpCode < 302) {
        return $httpCode;
    } else {
        return $httpCode;
    }
}

But, all I get is 301 from all the websites :/ return $httpCode is to check what I get.

I am trying this now

function getHttpResponseCode_using_curl($url, $followredirects = true){
        // returns int responsecode, or false (if url does not exist or connection timeout occurs)
        // NOTE: could potentially take up to 0-30 seconds , blocking further code execution (more or less depending on connection, target site, and local timeout settings))
        // if $followredirects == false: return the FIRST known httpcode (ignore redirects)
        // if $followredirects == true : return the LAST  known httpcode (when redirected)
        if(! $url || ! is_string($url)){
            return false;
        }
        $ch = @curl_init($url);
        if($ch === false){
            return false;
        }
        @curl_setopt($ch, CURLOPT_HEADER         ,true);    // we want headers
        @curl_setopt($ch, CURLOPT_NOBODY         ,true);    // dont need body
        @curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);    // catch output (do NOT print!)
        if($followredirects){
            @curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,true);
            @curl_setopt($ch, CURLOPT_MAXREDIRS      ,10);  // fairly random number, but could prevent unwanted endless redirects with followlocation=true
        }else{
            @curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,false);
        }
//      @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5);   // fairly random number (seconds)... but could prevent waiting forever to get a result
//      @curl_setopt($ch, CURLOPT_TIMEOUT        ,6);   // fairly random number (seconds)... but could prevent waiting forever to get a result
//      @curl_setopt($ch, CURLOPT_USERAGENT      ,"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");   // pretend we're a regular browser
        @curl_exec($ch);
        if(@curl_errno($ch)){   // should be 0
            @curl_close($ch);
            return false;
        }
        $code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); // note: php.net documentation shows this returns a string, but really it returns an int
        @curl_close($ch);
        return $code;
    }

And I tried it on this https://face.gta.world/sign-in/ which visually you can see a 500 error, the code above returns a 200 error

Marky
  • 61
  • 1
  • 7
  • `if ($httpCode >= 200 && $httpCode < 302)` and you say that all you get is 301; that's what this does. You may want to use an `OR` operator instead. However, this could depend which url's you're checking for, and we don't know what those are. – Funk Forty Niner Oct 26 '17 at 23:40
  • Possible duplicate of [PHP CURL follow redirect to get HTTP status](https://stackoverflow.com/questions/8681915/php-curl-follow-redirect-to-get-http-status) – Devon Bessemer Oct 26 '17 at 23:41
  • Add few more code in the answer above – Marky Oct 26 '17 at 23:47
  • *"which visually you can see a 500 error"* - check your logs then. – Funk Forty Niner Oct 26 '17 at 23:48
  • Fixed by faking a real browser `@curl_setopt($ch, CURLOPT_USERAGENT ,"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");` – Marky Oct 26 '17 at 23:50

1 Answers1

0

Try this

$url= 'http://google.com';

function get_http_response_code($url) {
  $headers = get_headers($url);
  return substr($headers[0], 9, 3);
 }

$get_http_response_code = get_http_response_code($url);

echo $get_http_response_code;
Muhammad Usman
  • 10,039
  • 22
  • 39