Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get friends in order of number of mutual friends?

I am developing a Facebook application. I want to know how I can get friends in the order of number of mutual friends. Is it possible with FQL or any other method?

like image 955
Kristen Harrison Avatar asked Oct 16 '11 08:10

Kristen Harrison


2 Answers

Update

I can't find a way to do it in one request using graph API but it can be done in Ruby by getting friends list then sending request for each friend using User context like this example

Original answer

Here is the FQL query to be used Which is working only for api versions < v2.1

SELECT uid, mutual_friend_count from user where uid in (SELECT uid2 FROM friend WHERE uid1=me()) ORDER BY mutual_friend_count desc

you can try it in the explorer

https://developers.facebook.com/tools/explorer?method=GET&path=fql%3Fq%3DSELECT%20uid%2C%20mutual_friend_count%2C%20friend_count%20from%20user%20where%20uid%20in%20%28SELECT%20uid2%20FROM%20friend%20WHERE%20uid1%3Dme%28%29%29%20ORDER%20BY%20mutual_friend_count%20desc

like image 143
Moustafa Samir Avatar answered Nov 01 '22 09:11

Moustafa Samir


Theoretically, you can get a list of a user's friends using:

$friendsOfFriend = $facebook->api('/'.$yourFriendsFacebookId.'/friends');

Then you can check each of the result to see if they are your friend too.

$isMyFriend = $facebook->api('/me/friends/'.$someonesFacebookId);

... and keep a track of the count.

However my test didn't return any result yet. I attempted to get the friends of some of my facebook friends but it returns an exception: Can't lookup all friends of {friend's_facebook_ID}. Can only lookup for the logged in user {my_facebook_ID}, or friends of the logged in user with the appropriate permission. So there might be permission issue here.

like image 31
Salman Avatar answered Nov 01 '22 09:11

Salman