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."'";
	
	$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 use


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

// 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
Comments
7 discussions around Display Facebook likes / Shares PHP function
  1. 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.

  2. dfghdhfg says:

    yhgfhdfghfghfghgf

  3. Cube3x says:

    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: http://cube3x.com/get-social-share-counts-a-complete-guide/

  4. DaveBowman says:

    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 says:

      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!

      Ashley

  5. 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?





Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.