Get Facebook Likes and Shares via Graph API


Facebook Like and Share count can easily be obtained from the Facebook Graph API. This is an extremely quick way to look up the JSON response from Facebook so that you can further process it for your own requirements.
<?php
$URL ='https://graph.facebook.com/cocacola';
$JSON = file_get_contents($URL);
$Output = json_decode($JSON);
$Likes = 0;
if($Output->likes){
  $Likes = $Output->likes;
}

echo "Number of likes for CocaCola's Facebook Page = ".$Likes."</br>";

$URL2 ='https://graph.facebook.com/http://www.coca-cola.com';
$JSON2 = file_get_contents($URL2);
$Output2 = json_decode($JSON2);
$Shares = 0;
if($Output2->shares){
  $Shares = $Output2->shares;
}

echo "Number of shares for coca-cola.com = ".$Shares;
?>

UPDATE 29/01/2012: I’ve written a slightly different method using the PHP SDK in this post here

4 thoughts on “Get Facebook Likes and Shares via Graph API”

  1. Great method to do this – displayed in a very simple way.
    Thanks a lot, you’ve really cut straight to the point of what I was needing!
    Something I’ve noticed is that in your code example your quotes and inverted commas are formatted funny, when I copied and pasted I had to change them to the other kind.

  2. Hi Loic,
    Good work there, that makes it very simple for Facebook page owners to get some useful information.
    I’m not really doing much FB development these days but good to see what you’re up to.

Leave a Comment

Your email address will not be published. Required fields are marked *