FQL Graph API: Mutual Friends

邮差的信 提交于 2019-12-31 07:07:13

问题


I know you can't get friends of friends, but can you get mutual friends using some query, in a computationally efficient manner?

Could I even iterate through my friends to see which of them is friends with a target? Is there any good way to get Mutual friends?


回答1:


You can do it like this:

$facebook = new Facebook(array(
  'appId'  => FB_ID,
  'secret' => FB_APP_SECRET,
  'cookie' => true,
));


$fql = "SELECT id FROM profile WHERE id IN (SELECT uid2 FROM friend WHERE uid1=me())";

$response = $facebook->api(array(
'method' => 'fql.query',
'query' =>$fql,
));

This will return the id of the mutual friends. Hope this helps you :)

EDIT:

If you want to get the mutual friends between you and id=13245 then you can do like:

$facebook = new Facebook(array(
  'appId'  => FB_ID,
  'secret' => FB_APP_SECRET,
  'cookie' => true,
));

$fql = "SELECT uid1, uid2 FROM friend WHERE uid1 IN (SELECT uid2 FROM friend WHERE uid1=me() AND uid2 = '13245' ) AND uid2 IN (SELECT uid2 FROM friend WHERE uid1=me())";

$response = $facebook->api(array(
'method' => 'fql.query',
'query' =>$fql,
));

It will return all mutual friends id.

Hope this gives you what you want.




回答2:


This might be old, i needed to calculate mutual friends between user X and me and found that @Sabari answer useful, except it had a mistake (unnecessary inner query)

SELECT uid2 FROM friend WHERE uid1=me() AND uid2 = '13245' )

This inner query will give one result ( 13245 ) as the friend table has two columns (both compose the primary key of the table) and the where clause is putting a condition on both columns.

so the whole query after removing this inner query should be:

SELECT uid1, uid2 FROM friend where uid1='13245' and uid2 in (SELECT uid2 FROM friend where uid1=me())

This query calculates a list of all '13245' friends ( where uid1='13245' ) who are also my friends (the inner query)

here is my code to get the list of mutual friends in python

def get_friends_ids(id):
    query = "select uid1,uid2 from friend where uid1=\"" + str(id) + "\" and uid2 in (select uid2 from friend where uid1=me())"
    query = urllib2.quote(query)
    url = "https://graph.facebook.com/fql?q=" + query + "&access_token=" + access_token
    data = urllib2.urlopen(url).read()
    j = json.loads(data)
    ids = []
    for record in j['data']:
        ids.append(record['uid2'])
    return ids



回答3:


I had same problem. Though it is very late for answering question, it will help somebody. That's why answering this question.

Try this fql query:

$query="SELECT uid, name, work, education FROM user WHERE uid!=$source_id AND uid IN
                    (SELECT uid1, uid2 FROM friend WHERE uid1 IN (SELECT uid2 FROM friend WHERE uid1=$target_id AND uid2 = '$source_id' )
                        AND uid2 IN (SELECT uid2 FROM friend WHERE uid1=$target_id)) ORDER BY name";

$user_info=$this->facebook->api(array('method'=>'fql.query',
                'query'=>$query));

By this we can get mutual friends info.



来源:https://stackoverflow.com/questions/9074184/fql-graph-api-mutual-friends

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