Tuesday, May 27, 2014

How to get the total facebook friends in PHP

This is just a simple script that will retrieve the total facebook friends of specific user using the graph api. We will be using graph api along with the FQL to get the friend count of the user and by doing that we need the facebook id which we can retrieve by authorizing our app with facebook API.

The output is on JSON which we can decode via PHP, please see sample below.
{ "data": [ { "friend_count": 740 } ] }

<?php 
$fbUserId = <your facebook id>;
$json = file_get_contents("https://graph.facebook.com/fql?q=SELECT%20friend_count%20FROM%20user%20WHERE%20uid=$fbUserId");
$json = json_decode($json);
echo @$json->data[0]->friend_count;
?>


Hope this helps, please leave a comment if you like this post.

No comments:

Post a Comment