How to read facebook like count for a url with PHP


/ Published in: PHP
Save to your folder(s)

When you add facebook like button to your site, probably, you also want to save the number of likes of your pages to your database.These data are very useful and very important because they tell you which of your articles are better than others. Moreover, these pages are better than others because the user has decided to tell his friends about it.


Copy this code and paste it in your HTML
  1. function readFacebookLikes($url) {
  2. $query = "select total_count from link_stat WHERE url ='" . $url ."'";
  3. $s = file_get_contents("https://api.facebook.com/method/fql.query?query=".
  4. urlencode($query)."&format=json");
  5. preg_match("#(\"total_count\"):([0-9]*)#",$s,$ar);
  6. if(isset($ar[2])) return $ar[2]; else return null;
  7. }

URL: http://www.barattalo.it/2012/10/08/how-to-read-facebook-likes-count/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.