and like us on FB

The purpose of this post is to show you how to determine the status (primarily online or offline) of a remote webpage using cURL and other methods. This post might be best read in company with another article “Resolve Short URLs To Their Destination URL with PHP“. For the sake of it, a small WordPress shortcode function will follow at the end of the post that will cache the status (online or offline) of a remote website for a period of 10 minutes.

Online or Offline

Let’s assume we wanted to check on the status of this website by reading the returned HTTP header. This function uses curl_getinfo to retrieve the header response code that it uses to determine the status of the remote website.

Usage:

The line if($httpcode >= 200 && $httpcode < 300) – and, in particular, the 300 code – can be modified to return the status of the remote website based on your own requirements (maybe just 200).

A full list of header codes is available on our reference page. Clients can download the header codes in various array formats.

The status range if fairly generic in nature and doesn’t give us a complete picture of the remote site. For that reason, I return the remote HTTP status code and return a more applicable message (see the function below).

Remote Status Code

The following code will return only the remote header status code. It’s essentially identical to the function above with the exception that we’re returning a code rather than testing a range.

Usage:

Returns: 200

You can read more about the various HTTP header codes here (W3.org), here (Wikipedia), or our reference page. Based on the return code you could easily create custom check messages.

Generally speaking:

1xx header messages are informational.
2xx header messages indicate a success.
3xx header messages indicate a redirect.
4xx header messages indicate a client error.
5xx header messages indicate a server error.

Array of Header Data

To return an array of remote header data, you could use something like this:

Usage:

Returns:

Determine the Status of a Remote Webpage and Retrieve the HTTP Status Code

In the above example, I’ve also directly printed the Server Response from the array ($RemoteHeaderArray[0]).

Other Methods To Check Remote Website

There are countless means of checking the status of a remote website or retrieving remote header data. Here’s just a few different ways of accomplishing the same thing we’ve done up the top of this page with cURL.

PHP’s get_headers() Function

The following will use PHP’s get-headers() function and return Online or Offline (with online determined by a 200, 301 or 302 header status code).

Usage:

Returns: Online.

Using the same function you can just as easily return an array of the remote header.

Returns:

Array
(
[0] => HTTP/1.0 200 OK
[1] => Date: Sun, 26 Aug 2012 05:49:33 GMT
[2] => Server: Apache
[3] => X-Pingback: http://www.url.com/xmlrpc.php
[4] => Set-Cookie: wpfilebase=1
[5] => Vary: Accept-Encoding
[6] => Connection: close
[7] => Content-Type: text/html; charset=UTF-8
)

PHP’s $http_response_header & stream_get_meta_data() Function

PHP’s stream-get-meta-data() and $http_response_header can also be used to obtain header data.

When using a HTTP wrapper , $http_response_header will be populated with the HTTP response headers (similar to get_headers()).

Both are less efficient than PHP’s get_headers() function. You could also consider using PHP’s getallheaders() , headers_list() or apache_request_headers() function. There are other PHP Network Functions that may also serve your particular purpose.

PHP’s fsockopen() Function

By opening a socket with fsockopen() and checking for a connection we can obtain limited data. It’s handy if your server doesn’t support cURL.

Usage:

Returns: Online.

WordPress Shortcode

Should you choose to reference the status of a remote website from within a WordPress post or page, you can use the following shortcode.

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

Usage:

If you’ve define a default URL in your shortcode attributes, you can simply use [httpstatus]. The shortcode will return text as defined in your attributes (Online or Offline). Should you choose to reference another URL, you would simply do so as follows: [httpstatus url=”http://www.flight.org/”].

Available shortcode attributes are $cache time, $online message, and $offline message.

PHP Function (Simple Cache)

A working PHP function to be used outside of WordPress is as follows. Usage of Simple Cache is required.

Considerations

  • Consider replacing online/offline text with images.
  • You should consider caching the responses for at least a few minutes (as we’ve done with our WP and final PHP function) to avoid making repeated requests to the same site.
  • Randomize user agents with this function.

Download

Title: Determine the HTTP Status of a Remote Website
Description: Determine the Status of a Remote Webpage and Retrieve the HTTP Status Code.
Download: Shortcode (V0.2) | WP Plugin (V0.2) | PHP Code (V0.2)

Shortt URL for this post: http://shor.tt/2GKw