"RT @SoVeryBritish: Raising your eyebrows slightly to indicate the emotional roller coaster you're currently riding"

@ashleyford 3 days ago

"@Eva_Shaughnessy works for me..."

@ashleyford 2 weeks ago

Designer and web developer, Co-founder and Technical Director at Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]


Display Facebook likes / Shares PHP function

Use PHP to get the Facebook likes, shares and comments count for a specific URL


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;

Designer and web developer, Co-founder and Technical Director at Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]

Comments 8
  • Pablo Del Rio
    Posted on

    Pablo Del Rio Pablo Del Rio

    Reply Author

    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
    Posted on

    Online Form Generator Online Form Generator

    Reply Author

    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
    Posted on

    dfghdhfg dfghdhfg

    Reply Author


  • Cube3x
    Posted on

    Cube3x Cube3x

    Reply Author

    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
    Posted on

    DaveBowman DaveBowman

    Reply Author

    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
      Posted on

      Ashley Ashley

      Reply Author

      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 😛 i need to disable that for if you’re commenting.

      Good catch. Thanks!


  • Michael Davis
    Posted on

    Michael Davis Michael Davis

    Reply Author

    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?