Is there a simple PHP or Javascript method of getting the total number of FaceBook friends? Twitter makes it super easy to do that, and I need to do the same for one of my WordPress installs.
4 Answers
To get your friends as a JSON object from Facebook, you can use their Graph API. Easiest way is to visit this page:
http://developers.facebook.com/docs/reference/api/user/
Scroll down until you find the Friends link in the “Connections” table. That link will give you a JSON object containing all your friends. Note that the URL on that page is unique to you and contains your access token. Don’t share the link.
You can use this sort of code in WP to get and use that information:
$body = wp_remote_retrieve_body(wp_remote_get('YOUR_FB_URL', array('sslverify'=>false)));
$dec = json_decode($body);
echo count($dec->data);
You’ll want to use transients or something similar to cache the data so that you don’t ask FB for it all the time.