我正在使用以下代码检索用户的好友列表以及这些好友的信息。
$frnd = $facebook ->api('/me/friends?access_token='.$accessToken.'&fields=id,name,about,hometown');
for ($i=0; $i<$nr_friends; $i++)
{
$friendinfo = $facebook->api('/'.$frnd["data"][$i]["id"].'?fields=id,name,hometown,location');
echo $friendinfo['name']." ".$friendinfo['location']['name']." ".$friendinfo['hometown']['name']."</br>";
}
问题是,此方法需要花费大量时间来加载好友信息。有没有办法让这更快?
答案 0 :(得分:2)
我认为您可以通过将ID作为ids
参数传递,将查询朋友的信息压缩到一个API中。我没有对此进行测试,但尝试类似以下内容:
// get a list of your your friends' IDs
$friends = $facebook->api('/me/friends?access_token='.$accessToken.'&fields=id');
// condense those IDs into a comma-separated string
$friends_ids = implode(',', $friends);
// now query for friends' name, hometown and location
$friends_info = $facebook->api('/?access_token='.$accessToken.'&fields=id,name,hometown,location&ids='.$friends_ids);
我不确定自己的确切语法,但您应该在此处找到更多信息:http://developers.facebook.com/docs/api#reading
答案 1 :(得分:2)
使用FQL语言:
$facebook->api(array('method'=>'fql.query','query'=>"SELECT uid,name,first_name,middle_name,last_name,pic_square,hometown_location,current_location,profile_url,email,website FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"));
有关具有相应列名的所有表的列表,请参阅:Facebook FQL Reference
答案 2 :(得分:2)
<?php
$friends = $facebook->api('me/friends');
//print_r($friends['data']);
print_r("Number of Friends: ". count($friends['data']));
foreach ($friends['data'] as $key=>$listOfFriends) {
echo "<br/>".$key." ".$listOfFriends['name']."<img src='https://graph.facebook.com/".$listOfFriends['id']."/picture' width='50' height='50' title='".$listOfFriends['name']."' />";
}
?>