You are here

function fb_get_friends in Drupal for Facebook 5

Same name and namespace in other branches
  1. 5.2 fb.module \fb_get_friends()
  2. 6.3 fb.module \fb_get_friends()
  3. 6.2 fb.module \fb_get_friends()
  4. 7.3 fb.module \fb_get_friends()

A convenience method for returning a list of facebook friends. This should work efficiently in canvas pages for finding friends of the current user. In other cases it tries to work, but will be an expensive operation and only succeed when the user has created an infinite session.

@return: an array of facebook ids

4 calls to fb_get_friends()
fb_devel_fbu_page in ./fb_devel.module
A page which tests function which work with facebook user ids
fb_node_grantsXXX in ./fb.module
Implementation of hook_node_grants.
fb_user_get_local_friends in ./fb_user.module
Returns local uids of friends of a given user.
fb_views_handler_author_is_friend in ./fb_views.module

File

./fb.module, line 338

Code

function fb_get_friends($fbu, $fb_app = NULL) {
  static $cache = array();

  // Facebook only allows us to query the current user's friends, so let's try
  // to log in as that user.  It will only actually work if they are the
  // current user of a canvas page, or they've signed up for an infinite
  // session.
  $fb = fb_api_init($fb_app, $fbu);
  if (!$fb || !$fbu) {
    return;
  }
  if (!isset($cache[$fbu])) {
    if ($fb === $GLOBALS['fb'] && $fbu == fb_facebook_user($fb)) {
      $items = $fb->api_client
        ->friends_get();
    }

    // friends_get does not work in cron call, so we double check.
    if (!$items || !count($items)) {
      $logged_in = fb_facebook_user($fb);
      $query = "SELECT uid2 FROM friend WHERE uid1={$fbu}";
      $result = $fb->api_client
        ->fql_query($query);
      fb_report_errors($fb);
      $items = array();
      if (is_array($result)) {
        foreach ($result as $data) {
          $items[] = $data['uid2'];
        }
      }
    }

    // Facebook's API has the annoying habit of returning an item even if user
    // has no friends.  We need to clean that up.
    if (!$items[0]) {
      unset($items[0]);
    }
    $cache[$fbu] = $items;
  }
  return $cache[$fbu];
}