facebookfacebook-graph-apifacebook-fqlfql.multiquery

Friend with the highest number of mutual friends


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?


Solution

  • 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);
    }