Get the number of Facebook Likes using PHP


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

Use the facebook api to grab how many \\\'likes\\\' a url has had.


Copy this code and paste it in your HTML
  1. <?php
  2. // the url to check how many likes
  3. $url = 'http://www.likewizard.com/like-67';
  4. // build the facebook query
  5. $fburl = "http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url='$url'&format=atom";
  6. // grab the atom dump via facebook api url call above
  7. $ch = curl_init($fburl); // url for page
  8. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  9. $atom_data = curl_exec($ch);
  10. // it returns something like this:
  11. /* <fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
  12.   <link_stat>
  13.   <like_count>9</like_count>
  14.   </link_stat>
  15. </fql_query_response> */
  16. // grab the like count out, i hate dom parsing, so just use regex:
  17.  
  18. preg_match('#like_count>(\d+)<#',$atom_data,$matches);
  19. $like_count = $matches[1];
  20. echo "The URL $url has $like_count likes on facebook";
  21.  
  22.  
  23.  
  24. // OPTION 2 >>> keeping it to a 1 liner:
  25. $data = json_decode(file_get_contents("http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url='$url'&format=json"));
  26. echo "The URL $url has " . $data[0]->like_count . " likes on facebook";
  27.  
  28. ?>

URL: http://codecanyon.net/forums/thread/help-atom-feed-to-database/28994

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.