Posted By

dtbaker on 07/19/10


Tagged

facebook


Versions (?)

Who likes this?

12 people have marked this snippet as a favorite

ahmadassaf
Vordreller
osirisinternet
khawaib
manavecplan
phpdiva
rafaeluzzi
ninjacharlee
voove
sbcjr
ringo380
clacwebstudio


Get the number of Facebook Likes using PHP


 / Published in: PHP
 

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

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

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

Report this snippet  

You need to login to post a comment.