How Many Times A URL Has Been Liked/Shared On Facebook

Facebook keeps track of every likes, shares and comments for URLs. You can ask for theses counts in order to see how much a URL has been seen on Facebook.

Facebook is more and more closing public access to their API (this blog post on Facebook developers blog). So even if you can make the request directly, you should do as following and use the Facebook PHP SDK with apps credentials to make API calls.

If you don't have a Facebook app registered or you don't know what that means, go to facebook.com/developers, click on "Set Up New App" and follow the wizard. You are given an App ID and an App Secret that we will be using.

Important: make sure you filled "Site URL" and "Site Domain" with your infos.

We are using the Facebook PHP SDK (see on github) to make API calls :

require "facebook.php";
$facebook = new Facebook(array(
    'appId'  => YOUR_APP_ID,
    'secret' => YOUR_APP_SECRET,
));

$fql = 'SELECT url, share_count, like_count, comment_count, total_count 
        FROM link_stat WHERE url="http://stackoverflow.com"';

$result = $facebook->api(array(
    'method' => 'fql.query',
    'query' => $fql,
));

The $result array contains all the info you need, and even more :

Array (
    [0] => Array (
        [url] => http://stackoverflow.com
        [share_count] => 1356
        [like_count] => 332
        [comment_count] => 538
        [total_count] => 2226
    )
)

You have here :

  • share_count : the number of times this URL has been shared on Facebook

  • like_count : the number of times this URL has been liked (either from a share or from a like or recommend button on the page)

  • comment_count : the count of comments below the shares

  • total_count : the sum of the three last counts

I wrote this page after this answer on Stackoverflow I made.

Quentin Pleplé
June 2011