Google+ Tools
Make Google+ profile picture
Make Google plus banners for profile
Create and share your Google Plus profile banners.

Profile image for mrk studios dtbaker on July 19, 2010

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

Language
PHP
Tags

Get the number of Facebook Likes using PHP


<?php
// the url to check how many likes
$url = 'http://www.likewizard.com/like-67';
// build the facebook query
$fburl = "http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url='$url'&amp;format=atom";
// grab the atom dump via facebook api url call above
$ch = curl_init($fburl); // url for page
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$atom_data = curl_exec($ch);
// it returns something like this:
/* <fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true"> 
  <link_stat> 
    <like_count>9</like_count> 
  </link_stat> 
</fql_query_response>  */
// grab the like count out, i hate dom parsing, so just use regex:

preg_match('#like_count>(\d+)<#',$atom_data,$matches);
$like_count = $matches[1]; 
echo "The URL $url has $like_count likes on facebook";



// OPTION 2 >>> keeping it to a 1 liner:
$data = json_decode(file_get_contents("http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url='$url'&amp;format=json"));
echo "The URL $url has " . $data[0]->like_count . " likes on facebook";

?>

Comments

blog comments powered by Disqus