Search code examples
javascriptphpfacebook-graph-apireal-time

Facebook Page likes count with real time updates --> any better way?


Smiirl is a physical real time likes counter for a specific Fb page (www.smiirl.com/fr/).

I am wondering what is the best way to get the same result on a web page... I am using the php way with graph API and json decode, and then javascript refresh every 5 seconds... But I find it not reliable nor elegant.

PHP :

<!-- Header -->
$fb_id = "//YOUR FACEBOOK PAGE ID"
$facebook_token = "//YOUR TOKEN";    
$facebook_data = file_get_contents('https://graph.facebook.com/'.$fb_id.'?fields=likes&access_token='.$facebook_token);
$facebook_likes_var = json_decode($facebook_data);
$unformatted_likes = $facebook_likes_var->likes;
$facebook_likes = number_format($unformatted_likes, 0, ',', ' ');
<!-- Content -->
<?php echo $facebook_likes; ?>

JS :

<script type="text/javascript">
setTimeout(function(){
document.forms["reload"].submit();
}, 5000);
</script>

Any of you know a way to do without having to reload ? Anyway to get some sort of channel to listen to and a push notification from Facebook ?

Any example would be greatly appreciated ;)

Thanks for your help !

Vlad


Solution

  • That's what the Realtime webhooks are for: