我想找到我的朋友,与他们分享最多的共同朋友 我尝试使用FQL和图形API以下列方式:
获取当前登录用户的好友列表
FQL:SELECT uid1 FROM friend WHERE uid2="MY_USER_ID" and uid1 IN (SELECT uid1 FROM friend WHERE uid2=me())
图表API:$facebook->api('/me/friends?format=json&limit=5000')
对于列表中的每一个uid,我都可以获得共同朋友的列表并计算它。
FQL:SELECT uid1 FROM friend WHERE uid2="OTHER_USER" and uid1 IN (SELECT uid1 FROM friend WHERE uid2=me())
图表API:$facebook->api('me/mutualfriends/OTHER_USER')
HOWEVER ,通过所有朋友运行此操作需要 TONS ... 您是否熟悉更好的方法?
答案 0 :(得分:9)
如果您的目标只是获取共享朋友数量最多的朋友列表,即您不关心这些共同朋友是谁,那么实际上Geoff的FQL调用提供了太多信息,那么您需要
我还注意到Geoff的FQL返回了如此多的数据,Facebook实际上截断了数据。
此外,您可能希望在同一个FQl调用中获取这些朋友的姓名......
另一种看起来更好的FQL是:
SELECT name,mutual_friend_count FROM user WHERE uid IN(
SELECT uid2 FROM friend WHERE uid1=me())
这会返回朋友列表中共同朋友的数量。所以,如果你有500个朋友,你只能获得500个对象的回复。
答案 1 :(得分:4)
将Geoff的答案带到下一步,这是一个完整的PHP解决方案。
首先,这是Geoff的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())
这是用于检索JSON的PHP代码,并检查哪个朋友与您有最多的共同朋友。 (确保在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."
?>
答案 2 :(得分:3)
我使用了这样的查询来吸引共同的朋友:
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())
快速回复所有朋友的共同朋友。
答案 3 :(得分:0)
我不确定你到底想要什么。但如果您正在寻找列表中的热门朋友,您可以通过获取Feed来实现这一目标,然后根据帖子数量对朋友进行排名。
$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);
}