This tutorial will help you to get Facebook Likes, Shares and Comments of any Facebook Page or External Link. I have make a function fb_count() that will fetch Facebook likes, shares and comments from Facebook Database. Under this function we make a query that will get likes, shares and comments from facebook database and response us in form of JSON (JavaScript Object Notation). We use Facebook API, that will pass our query to facebook and then give us response. This script can be useful if you are developing social widget that will show your social links, you can show your likes though this script.


First we make a function that will pass one parameter and that parameter is our link.

function fb_count($url)

Then under this function first we make a query and ask it to SELECT share_count, like_count and comment_count from a table named link_stat. Obviously we need a KEY for for it so our Link or URL will be the KEY for this query.

$fql = "SELECT share_count, like_count, comment_count ";
$fql .= " FROM link_stat WHERE url = '$url'";

Then we use Facebook API to pass above Query. After passing this query we will get our response in JSON.

$fqlURL = "" . urlencode($fql);

When facebook response us in JSON we need to decode that JSON it can be through Buil-in function json_decode().

$response = file_get_contents($fqlURL);
return json_decode($response);

Now it’s time to pass our Link or URL to get likes, shares and comments. Simply I use finction fb_count() and provide it a parameter as a Link or URL.

$fb = fb_count('');

Wait a minute! There is nothing shown in page :'(

Keep Calm and Read

As I told you we get response in JSON so obviously we have to echo that response.

For facebook Shares Count

echo $fb[0]->share_count;

For Facebook Likes Count

echo $fb[0]->like_count;

For Facebook Comments Count

echo $fb[0]->comment_count;

That’s all!



Leave a Reply