Use your left/right keys to browse tutorials
Display Facebook likes / Shares PHP function

Display Facebook likes / Shares PHP function

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on June 18, 2012

We launched a new product at Harkable last week called Daily Pulse – which tracks social data for any URL, you can also subscribe to receive email reports. I thought i’d share a bit of functionality similar to how we engineer Daily Pulse.

The Code

Below is a super simple function that retrieves a JSON feed of comments, likes and shares for any URL you provide.

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."'";
	return json_decode($fb_json);

How to use

$fb = facebook_shares('');

// facebook share count
echo $fb[0]->share_count;

// facebook like count
echo $fb[0]->like_count;

// facebook comment count
echo $fb[0]->comment_count;

More tutorials from Papermashup
  • Pablo Del Rio

    Good, a query:
    And if I want to show how many “likes” has my Facebook post with PHP as I can do?

  • Online Form Generator

    Now, there are hundreds of programs available ranging from
    free to hundreds of dollars, including everything from a bare bones setup to
    an all inclusive image editing suit. Rather, they’re designed to help spark possibilities in your own mind. If a picture is worth a thousand words then you can just image how much you will absorb by browsing this site.

  • dfghdhfg


  • Cube3x

    Excellet article. Loved the layout of the website too especially the fixed red top bar. Here I have combined a list of api and examples to fetch social count data from various services like facebook, twitter, gplus etc. Have a look here:

  • DaveBowman

    Nice find, thanks!

    Noticed a bug: it’s good to free memory after calculations but
    unset [$fb_json] in the function will never be called.

    P.S. This is crazy – if I press “left” cursor button in the comment area, my Firefox (12) sets off to some other posts on this site.

    • Ashley

      Hi Dave,

      thanks for the heads up. I’ve removed that line now from the function. Also the left right arrows are setup to scroll through posts on the site. If you see just above the article there are next / prev buttons, but you’re right thats a bug :P i need to disable that for if you’re commenting.

      Good catch. Thanks!


  • Michael Davis

    Thanks for sharing this!

    Is the function grabbing the comments themselves, (is that comments_fbid), or just does it grab just the counts?

    If it’s just the counts, is it possible to grab the content of the comments in a similar way?

    • Ashley

      Hi Michael,

      I’ve not looked too deeply into retrieving comments, but have unearthed access to comments via the graph api.

      The above spits out a JSON feed which you should be able to use. You cqn pass any URL into the ids.

      Let me know if that helps!