Get Facebook Likes Count with PHP

Facebook Likes Count with PHP

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.

[button-blue url=”http://demos.eggslab.net/facebook-likes-count-php/” target=”_blank” position=””]DEMO[/button-blue][button-green url=”http://demos.eggslab.net/downloads/8″ target=”_blank” position=””]DOWNLOAD[/button-green]

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 = "https://api.facebook.com/method/fql.query?format=json&query=" . 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('http://www.9lessons.info/2015/03/audio-recording-with-custom-audio.html');

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!

[button-blue url=”http://demos.eggslab.net/facebook-likes-count-php/” target=”_blank” position=””]DEMO[/button-blue][button-green url=”http://demos.eggslab.net/downloads/8″ target=”_blank” position=””]DOWNLOAD[/button-green]


6 responses to “Get Facebook Likes Count with PHP”

  1. Fer (@finlayzon) Avatar

    Amazing, please share more demos like this with facebook api

  2. Hafiz Abdullah Majid Avatar

    Stay connected, we will.

  3. Ad * Avatar
    Ad *

    error in your demo. not working

  4. Abdullah Majid Avatar

    It’s working fine on my side 🙂

  5. Sam Avatar
    Sam

    Hi,

    your demo is unfortunately not working:

    https://cl.ly/3r3N290K2U1r

    Why do we get an fatal error here?

  6. Sam Avatar
    Sam

    I think the problem is that REST API is deprecated for versions v2.1 and higher.

    Here is what we get in return for the request:

    https://api.facebook.com/method/fql.query?format=json&query=SELECT+share_count,+like_count,+comment_count++FROM+link_stat+WHERE+url+=+%27https://www.facebook.com/tippsecret%27

    So i guess we need to update the fb api v.

Leave a Reply

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