Improve speed of listing Friends in FBML

My Facebook application “Jewish Birthday” listed all friends using the following code:

$friends = $facebook->api_client->friends_get();
foreach ($friends as $friend) {
  echo "
";
        $user_details = $facebook->api_client->users_getInfo($friend, 'last_name, first_name, hometown_location, birthday_date');
        echo $user_details[0]['first_name'].' '.$user_details[0]['last_name'];
}

This mechanism is very slow and doesn’t allow to return a list of friends more than 25. This happens due to limitation of users_getInfo() function.

So, you should limit it with inserting the following code:

$friends = $facebook->api_client->friends_get();
$friends_count = count($friends);
$friends_last_page = ceil($friends_count/25);
$friends = array_slice($friends, ($pageno-1)*25, 25);
foreach ($friends as $friend) {
echo "
";
        $user_details = $facebook->api_client->users_getInfo($friend, 'last_name, first_name, hometown_location, birthday_date');
        echo $user_details[0]['first_name'].' '.$user_details[0]['last_name'];
}

and adding scroll pages mechanism.

I looked for a solution how to filter friends’ list, and came to the idea of direct fql query request:

$friends = $facebook->api_client->fql_query("SELECT name,hometown_location, birthday_date,uid FROM user WHERE uid IN ( SELECT uid2 FROM friend WHERE uid1=$user_id) ");
foreach ($friends as $friend) {
        echo "";
        echo "";
}

The reasons of this behavior are the following:

  • users_getInfo() returns only Friends’ uids
  • each request to Facebook api takes pretty much time

Actually, listing friends speed grew very much after this fix.

Leave a Reply

%d bloggers like this: