...

View Full Version : checking if facebook is up or down help



Hoppus
04-18-2012, 09:49 PM
for some reason, i frequently can't connect to facebook (just facebook). not really sure what it is or why. but i'm trying to just have a personalized website where i can go to check it's up or down status.


<?php
ini_set("default_socket_timeout","05");
set_time_limit(5);
$f=fopen("http://www.facebook.com","r");
$r=fread($f,1000);
fclose($f);
if(strlen($r)>1) {
echo("<span class='online'>Online</span>");
}
else {
echo("<span class='offline'>Offline</span>");
}
?>

is what i'm working with. i've used and tested this script with other websites and it works a'ok. but when it comes to facebook, or some reason it won't work and i'm getting errors, such as:


Warning: fopen(http://www.facebook.com) [function.fopen]: failed to open stream: Redirection limit reached, aborting in /home/davidhoppus/public_html/supbr0.php on line 36

Warning: fread(): supplied argument is not a valid stream resource in /home/davidhoppus/public_html/supbr0.php on line 37

Warning: fclose(): supplied argument is not a valid stream resource in /home/davidhoppus/public_html/supbr0.php on line 38
Offline

thanks in advanced for the help.

byrondallas
04-18-2012, 10:59 PM
Why not use file_get_contents instead:


<?php
ini_set("default_socket_timeout","05");
set_time_limit(5);
$f = file_get_contents("http://www.facebook.com");
if(strlen($f)>1) {
echo("<span class='online'>Online</span>");
} else {
echo("<span class='offline'>Offline</span>");
}
?>

An even better way is to use curl if your server supports it:


<?php
$url = "http://www.facebook.com";
$agent = $_SERVER['HTTP_USER_AGENT'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLINFO_EFFECTIVE_URL, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$src = curl_exec($ch);
$extract = curl_getinfo($ch);
$httpcode = $extract['http_code'];
curl_close($ch);

if($httpcode >= 200 && $httpcode < 303)
{
echo "<font color=\"green\">Online</font><br>";
}
else
{
echo "<font color=\"red\">Offline</font><br>";
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum