Facebook Fanpage Like Count Without Authentication

Sep
27

Heres is a simple way to fetch the number of likes for a Facebook fanpage without authentication. The below implementation is to be run server-side using PHP, but this could be easily implemented using JavaScript as well.

<?php
function fetchFacebookLikes($username) {
   $url = "http://api.facebook.com/method/fql.query?format=json&query="
        . urlencode("SELECT fan_count FROM page WHERE username='$username'");
   $result = json_decode(file_get_contents($url));
   return $result[0]->fan_count;
}
echo fetchFacebookLikes("checkiday");
?>

Another alternative would be to use page_id='$username' instead of username='$username' in the query and pass the function a page id: 151259271601760.