How can I count the total friends of a Facebook us

2019-01-16 20:28发布

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

7条回答
混吃等死
2楼-- · 2019-01-16 20:54

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);
   }
});
查看更多
你好瞎i
3楼-- · 2019-01-16 20:57

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!

查看更多
Summer. ? 凉城
4楼-- · 2019-01-16 20:59

@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.

查看更多
冷血范
5楼-- · 2019-01-16 20:59

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.

查看更多
叼着烟拽天下
6楼-- · 2019-01-16 21:04

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.

查看更多
狗以群分
7楼-- · 2019-01-16 21:06

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

查看更多
登录 后发表回答