How can I count the total friends of a Facebook user by uid?
Asked Answered
G

7

17

How can I count the total friends of a Facebook user by uid?

Giuseppinagiustina answered 20/8, 2010 at 1:10 Comment(0)
A
35

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

SELECT friend_count FROM user WHERE uid = me()

Just replace me() with the UID of the user that you want to query.

Aflame answered 16/11, 2011 at 2:36 Comment(0)
C
14

@Scott

This very much works, I didn't expect it to :)

https://graph.facebook.com/fql?q=SELECT friend_count FROM user WHERE uid = 273103345

I tried with a random user id who is not a friend in Graph API. However for few random uids it returned null, I am guessing they were pages.

Corvus answered 11/1, 2012 at 8:37 Comment(1)
FQL is deprecated now.Latchstring
W
8

FQL is now deprecated as of October 2014. Quoting Facebook's FAQ :

As of Tuesday 22nd July 2014, we now return a total_count field within a summary property in the response to /v2.2/me/friends.

The user/friends endpoint does require the user_friends permission to return an actual list of friends who use your app, however the friend count field will still be returned without this the user_friends permission granted. Sample response:

    {
  "data": [
  ], 
  "summary": {
    "total_count": 811
  }
}

Full FAQ: https://developers.facebook.com/docs/apps/faq#total_friend_count

Wrapper answered 28/11, 2014 at 20:24 Comment(0)
M
3

COUNT is not supported by FQL:

SELECT uid2 
FROM friend 
WHERE uid1 = USERID

Since COUNT is not supported you need to get all of the records and then count them in whatever application that is making the SQL call.

If you want to get more information, like names, along with count you can use an inner select to get the friend's extended info:

SELECT name 
FROM user 
WHERE uid IN (SELECT uid2 
              FROM friend 
              WHERE uid1 = USERID)

Here is a related SO question:

FQL result count

References for Friend table:

Query this table to determine whether two users are linked together as friends.

Maura answered 20/8, 2010 at 1:18 Comment(0)
U
1

Heres my solution... works great!

$myfriends = $facebook->api('/XXXXXXX152466/friends');
$friendcount =  COUNT($myfriends['data']) + 1;

Replace XXXXXXX152466 with the facebook profile id#

Using Facebook PHP SDK along with Graph API!

Uraeus answered 31/12, 2010 at 22:19 Comment(1)
I think the result is a paginator, so I should it is not good if an user has a lot of friends.Poss
B
1

Starting with Facebook API 2.0 you can't get the full list of Facebook friends, only a partial list of friends that also authorized your app.

You can still get the total number of friends though, the Graph API user/friends has a field summary containing the count as total_count

So you can get it using:

FB.api("/me/friends", function (response) {
   if (response && !response.error) {
      console.log(response.summary.total_count);
   }
});
Brunell answered 12/12, 2016 at 9:26 Comment(0)
A
0

There are lot of way to count the no of friends using Facebook Graph API with Php SDK, out of one i am telling..

$fbuser=$fb->api('/me/friends');
$access_token = $fb->getAccessToken();


$friend_count=0;
foreach($fbuser['data'] as $friends)
{
  $friends_count++;
}

For complete tutorial visit this article Getting FB Friends Count

Adamski answered 12/2, 2015 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.