Friend with the highest number of mutual friends

こ雲淡風輕ζ 提交于 2020-01-02 07:53:13

问题


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:

  1. 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')

  2. 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?


回答1:


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.

$fql="SELECT actor_id FROM stream WHERE filter_key = 'others' AND source_id = me() ORDER BY actor_id LIMIT 3000";
$param=array(
            'method'    => 'fql.query',
            'query'     => $fql,
            'callback'  => ''
        );
        $fqlResult1   =   $this->facebook->api($param);
        $top_frds=array();
        foreach($fqlResult1 as $result1)
        {
            $top_frds[]=$result1['actor_id'];
        }


$new_array = array();
foreach ($top_frds as $key => $value) {
if(isset($new_array[$value]))
    $new_array[$value] += 1;
else
    $new_array[$value] = 1;
}
$top_frds=array();
foreach($new_array as $tuid => $trate)
{
$top_frds[]=array('uid'=>$tuid,'rate'=>$trate);
}



回答2:


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:

SELECT name,mutual_friend_count FROM user WHERE uid IN(
SELECT uid2 FROM friend WHERE uid1=me())

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.




回答3:


Taking Geoff's answer to the next step, here is a complete solution in PHP.

First, here was Geoff's FQL:

SELECT uid1, uid2 FROM friend  
  WHERE uid1 IN 
  (SELECT uid2 FROM friend WHERE uid1=me())
   AND uid2 IN 
  (SELECT uid2 FROM friend WHERE uid1=me())

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

<?php
$jsonurl = "https://api.facebook.com/method/fql.query?query=SELECT+uid1%2C+uid2+FROM+friend++%0A++WHERE+uid1+IN+%0A++%28SELECT+uid2+FROM+friend+WHERE+uid1%3Dme%28%29%29%0A+++AND+uid2+IN+%0A++%28SELECT+uid2+FROM+friend+WHERE+uid1%3Dme%28%29%29&access_token=***INSERTACCESSTOKENHERE***&format=json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json,true);

for ($friendship = 0; $friendship <= count($json_output); $friendship++)
{
    $firstfriend = $json_output[$friendship]["uid1"];
    $mutualfriendscount[$firstfriend] = isset($mutualfriendscount[$firstfriend]) ? $mutualfriendscount[$firstfriend] + 1 : 1;
}

$mostmutualfriends_count = 0;

foreach ($mutualfriendscount as $friend => $mutualfriendcount)
{
    if ($mutualfriendcount > $mostmutualfriends_count)
    {
        $mostmutualfriends_count = $mutualfriendcount;
        $mostmutualfriends_id = $friend;
    } 
}
echo "ID $mostmutualfriends_id has the most number of mutual friends with you: $mostmutualfriends_count."

?>



回答4:


I've used a query like this to get mutual friends:

SELECT uid1, uid2 FROM friend  
  WHERE uid1 IN 
  (SELECT uid2 FROM friend WHERE uid1=me())
   AND uid2 IN 
  (SELECT uid2 FROM friend WHERE uid1=me())

It returns all of your friends' mutual friends quickly.



来源:https://stackoverflow.com/questions/8376647/friend-with-the-highest-number-of-mutual-friends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!