Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 5 down vote accepted
+50

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.

share|improve this answer
    
In your code above, is the YOUR_FB_URL the url to the JSON object? –  Pippin Jan 19 '11 at 16:27
    
I haven't been able to get this to work? Any more feedback? –  Pippin Jan 21 '11 at 3:07
1  
this works fine, YOUR_FB_URL is the link you get from developers.facebook.com/docs/reference/api/user under connections table >> Friends . make sure you are logged in to facebook and you have the developers app in your profile. –  Bainternet Feb 2 '11 at 10:14
    
Yes, the YOUR_FB_URL would be replaced with the URL of the JSON object I pointed you to earlier. I tested this code, it does work. –  Otto Feb 3 '11 at 7:31
    
Thanks Otto, I trust you and will test for sure in a bit. –  Pippin Feb 7 '11 at 20:39

yes facebook does have a way for you to do that. you can use the facebook graph api and its user|friend connection

something like this:

first create an application at http://www.facebook.com/developers/ you might need to add the developer application to your profile and you will see a button "Set Up New App".

then download this facebook php wrapper from here https://github.com/facebook/php-sdk/blob/master/src/facebook.php

and then you can work with this PHP code:

<?php
//require the file you downloaded
require 'facebook.php';

$facebook = new Facebook(array(
                            'appId' =>'myid', //change to your APP ID
                            'secret'=>'mysecret',  //change to your SECRET code
                            'cookie'=>true,
                            ));

$session = $facebook->getSession();
$friends = $facebook->api('/me/friends');
$friends_count=0;
foreach($friends as $person){
 $friends_count = $friends_count + 1;
}

echo $friends_count;
?>

enjoy!

share|improve this answer
    
Hmm, that's not working. Do I need to change the /me/friends to anything? –  Pippin Jan 19 '11 at 19:29
1  
yes, and the appId and secret in the array up there too –  kaiser Feb 4 '11 at 1:18

You can get friend count by fql easily.

There is a friend_count field against the user table that you can query.

SELECT friend_count FROM user WHERE uid = me();

https://graph.facebook.com/fql?q=SELECT friend_count FROM user WHERE uid={any id}
share|improve this answer

I think... Look for permission of facebook.

e.g. url> http://developers.facebook.com/docs/authentication/permissions/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.