0

I am making a Facebook application for which I require the mutual friends of the logged in user. The Old API friends.getMutualFriends does the work but it is very slow. I was wondering if there is any other way of getting the list of mutual friends.

Moreover, applications like SocialGraph load very fast, so what functions are they using to get hold of the mutual friends so quickly?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114

3 Answers3

1

I found the solution :)
With friends.getMutualFriends, it takes n^3 API which is a huge number. I tried areFriends as well which was no better. So I fetched the data via FQL query as follows:

//Create Query
$params = array(
    'method' => 'fql.query',
    'query' => "  SELECT uid1, uid2 FROM friend 
WHERE uid1 IN 
(SELECT uid2 FROM friend WHERE uid1= $match)
AND uid2 IN 
(SELECT uid2 FROM friend WHERE uid1= $match)",
);

//Run Query
$result = $facebook->api($params);

It takes less than a second to get the mutual friends !

0

As of September 2011, there is a new api call for this:

This week we added a connection for the User object that allows you to get the list of mutual friends between two users. To use it, just get an access token for the current user and issue a GET request to:

https://graph.facebook.com/me/mutualfriends/FRIEND_ID

Platform Updates: Operation Developer Love

Michael
  • 1,919
  • 2
  • 16
  • 26
0

If you are concerned about speed, I would suggest calling the information once and finding a way to cache the data. Facebook now provides you with the ability to do so "legally". You may use memcached for this sort of thing or save it temporarily in a database.

Francis Pelland
  • 784
  • 4
  • 11
  • Thanks. Since I do not have access to my friends' friends so how can i call the information once. I have to run for loop to check mutual friends with all my friends. code: $friends = $facebook->api('/me/friends'); $friend = $friends['data']; foreach($friend as $j => $m ) { $param = array( 'method' => 'friends.getMutualFriends', 'source_uid' => $user, 'target_uid' => $m["id"], ); $mutualFriends = $facebook->api($param); } – mehwish nasim Aug 25 '11 at 13:07
  • In some cases, there will be limitations on what information you can display. Perhaps only show mutual friends when the user visit's that friend's profile? Otherwise, you are basically stuck doing what you are doing and updating the cache whenever it gets out of date. – Francis Pelland Aug 25 '11 at 13:44