I want to find my friend whom I share with them the highest number of mutual friends.
I tried to do it with FQL and graph API the following way:
Get the friends list of the current logged in user.
FQL:SELECT uid1 FROM friend WHERE uid2="MY_USER_ID" and uid1 IN (SELECT uid1 FROM friend WHERE uid2=me())
Graph API:$facebook->api('/me/friends?format=json&limit=5000')
For each one of the uid's in the list, I can get the list of mutual friends and count it.
FQL:SELECT uid1 FROM friend WHERE uid2="OTHER_USER" and uid1 IN (SELECT uid1 FROM friend WHERE uid2=me())
Graph API:$facebook->api('me/mutualfriends/OTHER_USER')
HOWEVER, it takes TONS of time to run this through all my friends...
Are you familiar with a better way to do that?
I've used a query like this to get mutual friends:
It returns all of your friends' mutual friends quickly.
I am not sure what exactly you want to acheive finally. But if you are looking for top friends in the list you can achieve that by fetching feed and then can rank friends according to number of posts.
Taking Geoff's answer to the next step, here is a complete solution in PHP.
First, here was Geoff's FQL:
And here is the PHP code to retrieve the JSON and check which friend has the most mutual friends with you. (Make sure you replace your access token in the URL.)
If your goal is only to get a list of friends with highest number of mutual friends, i.e., you do not care who those mutual friends are, then actually Geoff's FQL call provided way too much information then you need.
I also notice that Geoff's FQL returns so much data and Facebook actually truncates the data.
Besides, you might want to get the names of those friends in the same FQl call...
An alternative FQL that looks better is this:
This returns you the number of mutual friends from your friend list. So if you have 500 friends, you will only get a response with 500 objects.