2012-09-21

Twitter Widget is Off Line

If you use the Twitter Widget on your web site, you will know that sometimes the widget server goes off-line then your widget disappears.

Wouldn't it be nice if you could detect when the server was off-line and display an image of your widget with a status message, such as the one on the right.

Well, if your site runs on PHP, here is the code that will do just that. You will need to create your own 'widget off-line' image, but you can do that with Paint, or The Gimp (it's free).

Note: twitter_off_line.png is the name of the twitter image. 'media' is the directory that holds the image.

START OF CODE

//Check if twitter widget site is on-line. If not, display off-line image. The off-line image is a 'print screen' of the widget display. Ajust in-line style as required.
$headers=(@get_headers('http://widgets.twimg.com', 1));
if(!$headers)
{
    print '
    <IMG SRC="media/twitter_off_line.png" WIDTH="250" HEIGHT="200"     ALT="The Twitter widget is currently off-line." />';
}
else
{
    foreach($headers as $key=>$value)
    {
        if($key==0 && $value=='HTTP/1.1 200 OK')
        {
    print '
THE TWITTER WIDGET CODE GOES HERE';
        }
        else
        {
    print '
    <IMG SRC="media/twitter_off_line.png" WIDTH="250" HEIGHT="200" ALT="The Twitter widget is currently off-line." />';
        }
    break;
    }
}

END OF CODE

No comments:

Post a Comment