开发者

How to find friend's info faster using facebook php sdk

开发者 https://www.devze.com 2023-01-27 05:37 出处:网络
I\'m us开发者_如何学Pythoning the following code to retrieve the user\'s friends list and the info for those friends.

I'm us开发者_如何学Pythoning the following code to retrieve the user's friends list and the info for those friends.

$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>";
  }

The problem is, this method takes a lot of time to load the friends info. Is there any way to make this faster?


I think you can condense the querying of friends' info into one API by passing the IDs as an ids parameter. I haven't tested this, but try something similar to the following:

// 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);

I'm not sure on the exact syntax without testing myself, but you should find more information here: http://developers.facebook.com/docs/api#reading


<?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']."' />";     
    }

?>


Use the FQL language:

$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())"));

For a list of all the tables with their corresponding column names I refer to: Facebook FQL Reference

0

精彩评论

暂无评论...
验证码 换一张
取 消