Getting Facebook Likes, Shares and Comment Counts Using PHP Function

There are many api that gets the Facebook points like Shares, Likes, Comments and returns it. This Tutorial will get the Facebook likes, shares and comment counts of a specific URL using a php function. The function uses the FQL api of the Facebook and get the data using JSON. The PHP Function returns the Facebook Likes, Shares and Comment Counts. Here is the sample of FQL query to be used on facebook.

FQL Query:

$fql = "SELECT url, normalized_url, share_count, like_count, comment_count, ";

$fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";

$fql .= "link_stat WHERE url = '".$url."'";

The query will return the counts you want to get by a normal text. After this, you can get the returned data by using JSON. The JSON code should be like this:

JSON CODE:

$apifql="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql);

By using this, you can easily manage and design your Facebook points/stats depending on what you want or your theme designed and not limited on what the Facebook buttons has.

Now lets put it together and make it work. We will combine this in a function.

PHP CODE:


function facebook_shares($url){

$fql = "SELECT url, normalized_url, share_count, like_count, comment_count, ";
$fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";
$fql .= "link_stat WHERE url = '".$url."'";

$apifql="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql);
$fb_json=file_get_contents($apifql);
return json_decode($fb_json);

}

HOW TO DISPLAY IT 

$fb = facebook_shares('https://www.facebook.com/');

echo 'The URL https://www.facebook.com/ has ';
// facebook share count
echo ' fb shares:'.$fb[0]->share_count;

// facebook like count
echo ' fb likes:'.$fb[0]->like_count;

// facebook comment count
echo ' fb comments:'.$fb[0]->comment_count;

Thats it, we’ve get the facebook stat count of a certain URL. Hope that it helps you on your project.

Let others try and know this by sharing this tutorials. Thanks

DEMO DOWNLOAD  [tweegi-button name="tweetandgetit"]

Incoming search terms:

wilbert Santos

He is the founder and the man behind Upgraded Tutorials that focuses on creative and helpful web development tutorials. He is a Web Developer and knows CodeIgniter, WordPress and other frameworks. He loves web development and creating cool stuff. Add him on Facebook and Google

Worth Reading Topics

  • http://www.woolricha.com/ Woolrich

    Please tell me that youre heading to keep this up! Its so excellent and so important. I cant wait to read extra from you. I just really feel like you know so significantly and know how to make people listen to what you might have to say. This weblog is just also cool to become missed. Terrific things, genuinely. Please, PLEASE keep it up!

  • http://www.presstor.fr Presstor

    Thanks for your code.
    I adapted it to javascript to print the number of likes from my articles.

    Thanks a lot !

  • sdtom

    Thanks, this worked perfectly. The graph.facebook.com/[url] method for getting comment counts had stopped working recently, so it was great to find this.

  • mario

    how to count more facebook page likes in one number? tnx

  • http://www.facebook.com/joel.albers.7 Joel Albers

    Where in your code do you point to the account you are requesting data?
    Don’t you have to specify a user name and password?
    Thanks