Show Facebook Likes with PHP

Posted by: Alex on June 25, 2013

Here’s a simple way to show a facebook page’s current number of likes, it combines JSON and PHP, very simple! Can be used Facebook Profiles/Pages, as well as edited to work for general links and websites.

<?php
function fb_likes_count(){
$data = json_decode(file_get_contents("https://graph.facebook.com/guwii"));
echo $data->likes;}

fb_likes_count();
?>

At the time of writing that will output “91” (likes currently) straight away, just call the function fb_likes_count(); where you want it echo’d.
Example:
The number of likes on my Facebook page is: <?php fb_likes_count(); ?>

There is actually a lot more data available within that JSON object, here are the values returned that you can also hook onto if you wish:

{
   "about": "guwii - Web Design Agency in Cardiff.  https://guwii.com",
   "category": "Computers/technology",
   "is_published": true,
   "talking_about_count": 60,
   "username": "guwii",
   "website": "https://guwii.com",
   "were_here_count": 0,
   "id": "114477908707450",
   "name": "guwii",
   "link": "https://www.facebook.com/guwii",
   "likes": 91,
   "cover": {
      "cover_id": 199485470206693,
      "source": "http://sphotos-a.ak.fbcdn.net/hphotos-ak-ash3/s720x720/945532_199485470206693_1447682729_n.jpg",
      "offset_y": 0,
      "offset_x": 0
   }
}