2

I am working on a college website. I'm using a Facebook plugin to show the likes of a Facebook page by using this:

<iframe src="//www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Fpcclahore&amp;width=364&amp;height=220&amp;show_faces=true&amp;colorscheme=light&amp;stream=false&amp;border_color&amp;header=true" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:364px; height:220px;" allowTransparency="true"></iframe>

The problem is that Facebook is blocked on the college servers, so nothing appears except a warning. Is it possible to show a static image instead of Facebook plugin while the internet not available (localhost) or Facebook not accessible? Like this:

if(Facebook accessible)
show Facebook pluging
else
<img src="images/fb.jpg" width="364" height="220">
MECU
  • 770
  • 1
  • 11
  • 25
Natasha
  • 259
  • 4
  • 11
  • You can check the reply to this question http://stackoverflow.com/questions/1239068/ping-site-and-return-result-in-php – leticia Jan 17 '13 at 05:19
  • no you can't do this as far as you use iframe, for that you need to code at facebook's likebox.php page. – Dipesh Parmar Jan 17 '13 at 05:21
  • Are you sure Facebook is blocked and it is not a simple configuration issue? I find it rather ironic an IT based college would block something like Facebook. – kittycat Jan 17 '13 at 05:27
  • i suggest using javascript for this issue. Use ajax to request a page and see if facebook is blocked ( you'll probably get a redirect or some other error your college decides to show, you'll have to decide how to determine this). If it is blocked, replace the iframe with the image you want (http://api.jquery.com/replaceWith/). Doing this via php will not work since your script doesnt check the facebook connectivity through your college network – Nicolas Brown Jan 17 '13 at 05:40

4 Answers4

3
// check if local server is HTTPS if so check Facebook HTTPS
$remote = ('80' !== $_SERVER['SERVER_PORT'])
    ? array('host' => 'ssl://www.facebook.com', 'port' => 443)  // HTTPS Facebook
    : array('host' => 'www.facebook.com', 'port' => 80);        // HTTP Facebook

$fp = @ fsockopen($remote['host'], $remote['port'], $errno, $errstr, 5);

echo (!$fp) // check if failed
    ? '<img src="images/fb.jpg" width="364" height="220">' // output local image
    : '<iframe src="//www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Fpcclahore&amp;width=364&amp;height=220&amp;show_faces=true&amp;colorscheme=light&amp;stream=false&amp;border_color&amp;header=true" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:364px; height:220px;" allowTransparency="true"></iframe>'; // output IFRAME
kittycat
  • 14,983
  • 9
  • 55
  • 80
1

You have to use client-side JavaScript for this rather than server-side code. JavaScript allows you to check whether images can load successfully, so you could try using it to check whether Facebook is reachable by attempting to load an image from www.facebook.com (test page):

var testImg = new Image();

testImg.onload = function() {
  alert('facebook REACHABLE');
};

testImg.onerror = function() {
  alert('facebook UNREACHABLE');
};

testImg.src = '//www.facebook.com/images/fb_logo_small.png?' + new Date().getTime();

Then you can set the src attribute of your iframe accordingly.

You can alternatively put onerror on the iframe itself (and not use a separate test image); however, this will not work the college's content filtering system does not return an HTTP status indicating an error.

PleaseStand
  • 31,641
  • 6
  • 68
  • 95
  • 1
    This is a good method, but what happens if the image you are checking is say renamed, moved, etc? It will always come up as unreachable then. Facebook has a reputation for changing their site design often so the possibility of a chosen image always being there would be unreliable, right? – kittycat Jan 17 '13 at 06:01
0

You may try to ping the site first. If the result is positive show plugin else display a static image. set domain to facebook.com

 function pingDomain($domain){
   $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
   $status    = 0;

   if (!$file) $status = -1;  // show static image
   else {
   //show plugin
   }

 }
Engineer
  • 5,911
  • 4
  • 31
  • 58
  • fsockopen is not working on local host when internet is not connected.. it shows this error Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: No such host is known – Natasha Jan 17 '13 at 05:49
-1

You can check if facebook is available by fsockopen function of php.

ie,

   $check = fsockopen("www.facebook.com", 80, "error_no", "error_message", timeout in seconds);

    if (!$check) {

    //iframe

    } else {

   echo "<img src='images/fb.jpg' width='364' height='220'>"

    }

I didnt checked it works or not. Try it.

Vishnu R
  • 1,859
  • 3
  • 26
  • 45
  • fsockopen is not working on local host when internet is not connected.. it shows this error Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: No such host is known – Natasha Jan 17 '13 at 05:49
  • This issue will be resolved when you host your site. rgt? If u r irritated by this error in local, just disabling error_reporting will help u. – Vishnu R Jan 17 '13 at 06:02