Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

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

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.

1
<?php 
2
/*
3
 Determine the Status of a Remote Webpage and Retrieve the HTTP Status Code
4
 http://www.beliefmedia.com/determine-http-status
5
*/
6
 
7
function beliefmedia_remote_header($url){
8
   $agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)";
9
   $ch = curl_init();
10
   curl_setopt ($ch, CURLOPT_URL,$url );
11
   curl_setopt($ch, CURLOPT_USERAGENT, $agent);
12
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
13
   curl_setopt ($ch,CURLOPT_VERBOSE,false);
14
   curl_setopt($ch, CURLOPT_TIMEOUT, 5);
15
   curl_setopt($ch, CURLOPT_NOBODY, 1);
16
   curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
17
   curl_setopt($ch,CURLOPT_SSLVERSION,3);
18
   curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
19
   curl_exec($ch);
20
   $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
21
   curl_close($ch);
22
 
23
 if($httpcode >= 200 && $httpcode < 300) return true;
24
 else return false;
25
}
26
[/php]
27
 
28
<strong>Usage:</strong>
29
[php text="1"]echo (beliefmedia_remote_header('http://www.beliefmedia.com/') !== false) ? 'Online' : 'Offline';

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.

1
<?php 
2
/*
3
 Determine the Status of a Remote Webpage and Retrieve the HTTP Status Code
4
 http://www.beliefmedia.com/determine-http-status
5
*/
6
 
7
function beliefmedia_remote_status_code($url){
8
   $agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)";
9
   $ch = curl_init();
10
   curl_setopt ($ch, CURLOPT_URL,$url );
11
   curl_setopt($ch, CURLOPT_USERAGENT, $agent);
12
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
13
   curl_setopt ($ch,CURLOPT_VERBOSE,false);
14
   curl_setopt($ch, CURLOPT_NOBODY, 1);
15
   curl_setopt($ch, CURLOPT_TIMEOUT, 5);
16
   curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
17
   curl_setopt($ch,CURLOPT_SSLVERSION,3);
18
   curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
19
   curl_exec($ch);
20
 
21
   /* Other info returned via curl_getinfo here */
22
   /* http://php.net/manual/en/function.curl-getinfo.php */
23
 
24
  $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
25
  curl_close($ch);
26
 
27
 return $httpcode;
28
}

Usage:

1
$url = "http://www.internoetics.com/";
2
echo beliefmedia_remote_status_code($url);

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:

1
<?php 
2
/*
3
 Determine the Status of a Remote Webpage and Retrieve the HTTP Status Code
4
 http://www.beliefmedia.com/determine-http-status
5
*/
6
 
7
/* Get Header Array */
8
function beliefmedia_header_array($url) {
9
 
10
   $ch = curl_init("$url");
11
   curl_setopt($ch, CURLOPT_HEADER, 1);
12
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
13
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
14
   curl_setopt($ch, CURLOPT_NOBODY, 1);
15
   $yy = curl_exec($ch);
16
   curl_close($ch);
17
  $w = explode("\n",$yy);
18
 return $w;
19
}

Usage:

1
<?php 
2
$url = "http://www.beliefmedia.com/";
3
$RemoteHeaderArray = beliefmedia_header_array($url);
4
 
5
/* Wrap array in pre tags */
6
// print_r($RemoteHeaderArray);
7
echo 'Server Response is ' . $RemoteHeaderArray[0];

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).

1
<?php 
2
/*
3
 Determine the Status of a Remote Webpage and Retrieve the HTTP Status Code
4
 http://www.beliefmedia.com/determine-http-status
5
*/
6
 
7
/* Perhaps alter to boolean */
8
function beliefmedia_header_statuscode($url) {
9
 $headers = get_headers($url);
10
 return (preg_match('/^HTTP\/\d\.\d\s+(200|301|302)/', $headers[0])) ? 'Online' : 'Offline';
11
}

Usage:

1
$url = "http://www.beliefmedia.com/";
2
echo beliefmedia_header_statuscode($url);

Returns: Online.

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

1
<?php 
2
$url = "http://www.internoetics.com/";
3
$headers = get_headers($url);
4
 
5
/* Wrap in pre tags */
6
print_r($headers);

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()).

1
<?php 
2
$fp = fopen('http://www.flight.org/blog/', 'r');
3
/* Creates variable $http_response_header */
4
print_r($http_response_header);
5
/*  or... */
6
$meta_data = stream_get_meta_data($fp);
7
print_r($meta_data);

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.

1
<?php 
2
/*
3
 Determine the Status of a Remote Webpage and Retrieve the HTTP Status Code
4
 http://www.beliefmedia.com/determine-http-status
5
*/
6
 
7
function beliefmedia_socket_check_url($url) {
8
 $url = @parse_url($url);
9
 if (!$url) return false;
10
 
11
  $url = array_map('trim', $url);
12
  $url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
13
 
14
  $path = (isset($url['path'])) ? $url['path'] : '/';
15
  $path .= (isset($url['query'])) ? "?$url[query]" : '';
16
 
17
   if (isset($url['host']) && $url['host'] != gethostbyname($url['host'])) {
18
     $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
19
      if (!$fp) return false;
20
 
21
        fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
22
        $headers = fread($fp, 4096);
23
        fclose($fp);
24
 
25
  return (preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers)) ? 'Online' : 'Offline';
26
 
27
   } else {
28
  return 'Offline';
29
 }
30
}

Usage:

1
$url = 'http://www.beliefmedia.com/';
2
echo beliefmedia_socket_check_url($url);

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.

1
<?php 
2
/*
3
 Determine the Status of a Remote Webpage and Retrieve the HTTP Status Code
4
 http://www.beliefmedia.com/determine-http-status
5
*/
6
 
7
function beliefmedia_wp_check_status($atts) {
8
 
9
  $atts = shortcode_atts(array(
10
    'url' => 'http://www.beliefmedia.com/',
11
    'online' => 'Online',
12
    'offline' => 'Offline',
13
    'cache' => 600
14
  ), $atts);
15
 
16
 $hash = md5(serialize($atts));
17
 $transient = 'bmst_' . $hash;
18
 $cachedposts = get_transient($transient);
19
 
20
  if ($cachedposts !== false) {
21
   return $cachedposts;
22
 
23
    } else {
24
 
25
   $agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)";
26
   $ch = curl_init();
27
   curl_setopt($ch, CURLOPT_URL, $atts['url']);
28
   curl_setopt($ch, CURLOPT_USERAGENT, $agent);
29
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
30
   curl_setopt($ch, CURLOPT_VERBOSE, false);
31
   curl_setopt($ch, CURLOPT_TIMEOUT, 5);
32
   curl_setopt($ch, CURLOPT_NOBODY, 1);
33
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
34
   curl_setopt($ch, CURLOPT_SSLVERSION, 3);
35
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
36
   curl_exec($ch);
37
 
38
   $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
39
   curl_close($ch);
40
 
41
   $return = ($httpcode >= 200 && $httpcode < 300) ? $atts['online'] : $atts['offline'];
42
   set_transient($transient, $return, $atts['cache']);
43
 
44
  return $return;
45
 }
46
}
47
add_shortcode('httpstatus', 'beliefmedia_wp_check_status');

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.

1
<?php 
2
/*
3
 Determine the Status of a Remote Webpage and Retrieve the HTTP Status Code
4
 http://www.beliefmedia.com/determine-http-status
5
 Requires Simple Cache.
6
*/
7
 
8
function beliefmedia_wp_check_status($url, $args = '') {
9
 
10
  $atts = array(
11
    'online' => 'Online',
12
    'offline' => 'Offline',
13
    'cache' => 300
14
  );
15
 
16
 /* Merge $args with $atts */
17
 $atts = (empty($args)) ? $atts : array_merge($atts, $args);
18
 
19
 $hash = md5($url . serialize($atts));
20
 $transient = 'bmst_' . $hash;
21
 $cachedposts = beliefmedia_get_transient($transient, $atts['cache']);
22
 
23
  if ($cachedposts !== false) {
24
   return $cachedposts;
25
 
26
    } else {
27
 
28
   $agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)";
29
   $ch = curl_init();
30
   curl_setopt($ch, CURLOPT_URL, $url);
31
   curl_setopt($ch, CURLOPT_USERAGENT, $agent);
32
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
33
   curl_setopt($ch, CURLOPT_VERBOSE, false);
34
   curl_setopt($ch, CURLOPT_TIMEOUT, 5);
35
   curl_setopt($ch, CURLOPT_NOBODY, 1);
36
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
37
   curl_setopt($ch, CURLOPT_SSLVERSION, 3);
38
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
39
   curl_exec($ch);
40
 
41
   $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
42
   curl_close($ch);
43
 
44
   $return = ($httpcode >= 200 && $httpcode < 300) ? $atts['online'] : $atts['offline'];
45
   beliefmedia_set_transient($transient, $return);
46
 
47
  return $return;
48
 }
49
}
50
[/php]
51
 
52
<strong>Usage:</strong>
53
[php text="1"]/* Usage */
54
$url = 'http://www.beliefmedia.com/';
55
echo beliefmedia_wp_check_status($url);

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 (PHP)
Description: Determine the Status of a Remote Webpage and Retrieve the HTTP Status Code.
  Download • Version 0.1, 898.0B, zip, Category: PHP Code & Snippets
WordPress Plugins (General), (1.6K)    WordPress Shortcodes, (817.0B)    

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  AUS Eastern Standard Time (Connecticut)

  Want to have a chat?
 

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment

READY TO HAVE A CHAT? CALL US ANYTIME ON 1300 235 433