How can I count the total friends of a Facebook user by uid
?
问题:
回答1:
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.
回答2:
@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.
回答3:
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
回答4:
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.
回答5:
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!
回答6:
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
回答7:
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);
}
});