Check website availability with PHP and cURL

Perform a PHP cURL request to check if your website is up.

Here you’ll find a PHP script to check if your website is up and online available. It uses PHP and cURL. This script comes in handy because website uptime and availability is important, and you want your website to be always online available. If your website is down, it’ll send you an email to notify you about downtime.

Check if your website is up using PHP & cURL: a script to check the online status and availability

The PHP code to check if your website is up uses PHP cURL (Client URL Library). This function takes a domain name as input parameter and outputs TRUE or FALSE, for available or unavailable, depending on the returned HTTP status code.

Use this code to check if your website’s online availability. Schedule in a cron for automated checks.

Protip: learn why not to turn off CURLOPT_SSL_VERIFYPEER in your PHP configuration.

As you know, the HTTP status code 200 means OK, and 304 is Not Modified. Those are the status codes we are looking for as anything else may indicate a problem with your website or server.

<?php
/**
 * PHP/cURL function to check a web site status. If HTTP status 
 * is not 200 or 302, or the requests takes longer than 10 
 * seconds, the website is unreachable.
 * 
 * Follow me on Twitter: @HertogJanR
 * Send your donation through https://www.paypal.me/jreilink. Thanks!
 *
 * @param string $url URL that must be checked
 */

function url_test( $url ) {
	$timeout = 10;
	$ch = curl_init();
	curl_setopt ( $ch, CURLOPT_URL, $url );
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
	$http_respond = curl_exec($ch);
	$http_respond = trim( strip_tags( $http_respond ) );
	$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
	if ( ( $http_code == "200" ) || ( $http_code == "302" ) ) {
		return true;
	} else {
		// you can return $http_code here if necessary or wanted
		return false;
	}
	curl_close( $ch );
}

// simple usage:
$website = "www.example.com";
if( !url_test( $website ) ) {
	echo $website ." is down!";
} else {
	echo $website ." functions correctly.";
}
?>

Substitute www.example.com for your domain name. This can easily be scheduled in a Linux/Unix cron or Windows scheduled task, if you have PHP installed locally.

Read this too:   Add (custom) headers to MailMessage class

Send an email if your website is down

And what if your website is down? Send an email! :-) The above PHP function is very easy to extend with a PHP mail sending function. This’ll keep you informed on any downtime.

All can be automated using a monitoring tool like Monit. Read more about Monit website and services monitoring, built on an Ubuntu 14.04 LTS VM on Windows Server 2012 R2 Hyper-V.


Did this post helped you solve a problem? Or dit you find it interesting? Support Sysadmins of the North with a direct donation via Paypal of by bank wire transfer IBAN: NL31 ABNA 0432217258 (Jan Reilink). Just $ 5,- or € 5,- is more than enough, thanks!

Support SAOTN.ORG


7,653 views

10 comments

  1. Hi, using a browser sometimes returns another http response code then using curl on the same site. Maybe because of cookies to be accepted or user agents that are not set?

    1. Hi Dick, thank you for your comment.

      There is a whole range of status codes the webserver may return. Here is a list: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes :-) Request headers, cookies, user agents all can trigger a different return code. E.g a 302 Found for a particular browser, to redirect a visitor to a more appropriate version of the website content. Or the absent of a cookie could trigger a 401 Unauthenticated, meaning the visitor has to log on first.

  2. Thanks for sharing this. I’ve been trying to learn a little PHP. Great post for me and people who already use other languages like asp or asp.net and want to explore PHP. This is very helpful. Keep on the great work. I just want to say that there is no need to dive too deep in PHP if you don’t want to. A versatile coder can just pick what they need for a particular scenario.

  3. Hello, thanks for the snippet! A question: shouldn’t the “curl_close( $ch );” statement come before the “return” lines? Otherwise it’s not executed, is this right?

    1. Thanks for sharing. This code works perfectly fine in workspaces.
      For example:
      if ( ( $http_code == “200” ) || ( $http_code == “302” ) || ( $http_code == “301” ) || ( $http_code == “307” ) )

  4. I have a requirement to check if a particular url is down and then based on that do something. I have written similar code. And I have blocked the particular site on my local hosts file so that it returns a non success status code. Everthing seems to work in local environment. But when I deploy my changes to a QA server, the same code is not working. I am unable to block the particular URL. Where can I block the particular URL to see the similar effect of that of hosts file

  5. Great CURL setup!

    But cannot get it to work on subdomains?

    Works perfectly example.com but always shows test.example.com as being down.

    Any ideas how to make work with subdomains?

    1. Hi Susan,

      Maybe you receive a different HTTP status code than 200 or 302? Or an other failure? Try to dump $http_code using either:
      print_r( curl_getinfo( $ch, CURLINFO_HTTP_CODE ) );

      or simply

      echo $http_code; die(); above the line

      if ( ( $http_code == "200" ) || ( $http_code == "302" ) ) {

      For debugging only, remove when you’ve found the issue.

Leave a Reply

Your email address will not be published. Required fields are marked *