Search code examples
facebookfacebook-graph-apifacebook-fqlfacebook-php-sdkfacebook-friends

Get relationship (friend, friend of friend) between two Facebook ids by facebook api


How can I check for the relation between two facebook ids.? i.e friend, friend of friend or there is no relation. By fql or graph api methods(anything).

I know how to get the friend list, mutual friends of facebook id. But I want a proper method which can give the relation between two Facebook ids. I have Googled a lot but didn't get anything related to my problem.


Solution

  • I got solution by checking with fql queries.

    //$target_id=id of user to check relationship
    //$source_id=logged in user
    
        $query="SELECT uid, name, work FROM user WHERE uid IN 
        (SELECT uid2 FROM friend WHERE uid1 = $target_id AND uid2=$source_id)
         ORDER BY name";
    
        $user_info=$this->facebook->api(array('method'=>'fql.query',
                                    'query'=>$query));
    
    
        if(!empty($user_info)) 
        {
              $degree='friend';
        }
        else {
            $query="SELECT uid, name, work FROM user WHERE uid IN 
           (SELECT uid2 FROM friend WHERE uid1 IN (SELECT uid2 FROM friend WHERE uid1 = $target_id) AND uid2=$source_id) 
            ORDER BY name";
    
           $user_info=$this->facebook->api(array('method'=>'fql.query',
                                        'query'=>$query));
    
        // we can check friend-of-friend for app user friends
    
           if(!empty($user_info)) {
               $degree='friend-of-friend'; 
           }
        else{
        $degree='none';
        }
        }
    

    Please answer if anyone knows any simple method.