curl and ping - how to check whether a website is either up or down?

76,369

Solution 1

something like this should work

    $url = 'yoururl';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if (200==$retcode) {
        // All's well
    } else {
        // not so much
    }

Solution 2

curl -Is $url | grep HTTP | cut -d ' ' -f2

curl -Is $url outputs just the headers.

grep HTTP filters to the HTTP response header.

cut -d ' ' -f2 trims the output to the second "word", in this case the status code.

Example:

$ curl -Is google.com | grep HTTP | cut -d ' ' -f2
301

Solution 3

function checkStatus($url) {
    $agent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; pt-pt) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27";

    // initializes curl session
    $ch = curl_init();

    // sets the URL to fetch
    curl_setopt($ch, CURLOPT_URL, $url);

    // sets the content of the User-Agent header
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);

    // make sure you only check the header - taken from the answer above
    curl_setopt($ch, CURLOPT_NOBODY, true);

    // follow "Location: " redirects
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    // return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // disable output verbose information
    curl_setopt($ch, CURLOPT_VERBOSE, false);

    // max number of seconds to allow cURL function to execute
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);

    // execute
    curl_exec($ch);

    // get HTTP response code
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    if ($httpcode >= 200 && $httpcode < 300)
        return true;
    else
        return false;
}

// how to use
//===================
if ($this->checkStatus("http://www.dineshrabara.in"))
    echo "Website is up";
else
    echo "Website is down";
exit;

Solution 4

ping won't do what you're looking for - it will only tell you if the machine is up (and responding to ping). That doesn't necessarily mean that the webserver is up, though.

You might want to try using the http_head method - it'll retrieve the headers that the webserver sends back to you. If the server is sending back headers, then you know it's up and running.

Solution 5

Here is how I did it. I set the user agent to minimize the chance of the target banning me and also disabled SSL verification since I know the target:

private static function checkSite( $url ) {
    $useragent = $_SERVER['HTTP_USER_AGENT'];

    $options = array(
            CURLOPT_RETURNTRANSFER => true,      // return web page
            CURLOPT_HEADER         => false,     // do not return headers
            CURLOPT_FOLLOWLOCATION => true,      // follow redirects
            CURLOPT_USERAGENT      => $useragent, // who am i
            CURLOPT_AUTOREFERER    => true,       // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 2,          // timeout on connect (in seconds)
            CURLOPT_TIMEOUT        => 2,          // timeout on response (in seconds)
            CURLOPT_MAXREDIRS      => 10,         // stop after 10 redirects
            CURLOPT_SSL_VERIFYPEER => false,     // SSL verification not required
            CURLOPT_SSL_VERIFYHOST => false,     // SSL verification not required
    );
    $ch = curl_init( $url );
    curl_setopt_array( $ch, $options );
    curl_exec( $ch );

    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return ($httpcode == 200);
}
Share:
76,369
brainless
Author by

brainless

Once I was: WebMethods, Java developer, Middleware Engineer Then: PHP Developer, Laravel, Application Design and Architecture Today:Android Developer Tomorrow : May be mainframe! or start a restaurant!! SOreadytohelp

Updated on December 03, 2021

Comments

  • brainless
    brainless over 2 years

    I want to check whether a website is up or down at a particular instance using PHP. I came to know that curl will fetch the contents of the file but I don't want to read the content of the website. I just want to check the status of the website. Is there any way to check the status of the site? Can we use ping to check the status? It is sufficient for me to get the status signals like (404, 403, etc) from the server. A small snippet of code might help me a lot.

  • Fluffeh
    Fluffeh over 11 years
    When providing code that solves the problem, it is best to also give at least a short explanation of how it works so that folks reading won't have to mentally parse it line by line to understand the differences.
  • machineaddict
    machineaddict over 10 years
    I also prefer to set CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT as well to lower values.
  • m3nda
    m3nda about 9 years
    @machineaddict Do you mean to ouput "site offline" instead of wait for the connection? I don't see the difference between check 2 times for one thing and wait a few for the current operation.
  • Henry Harris
    Henry Harris almost 9 years
    @machineaddict Even with CURLOPT_FOLLOWLOCATION set to true, it still returns a 301 return code... How is this possible?
  • machineaddict
    machineaddict almost 9 years
    @HenryHarris: read this and this
  • carefulnow1
    carefulnow1 almost 7 years
    There are a lot more succcessful HTTP response codes than 200. en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
  • Funk Forty Niner
    Funk Forty Niner about 6 years
    This embedded hyperlink ca.php.net/http_head redirects to ca.php.net/… which the following message: http_head doesn't exist. Closest matches:. The answer needs to be edited in order to contain the correct reference.
  • girasquid
    girasquid almost 6 years
    @FunkFortyNiner Thanks, fixed the link