You are here

function fb_call_method in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 6.3 fb.module \fb_call_method()

This helper original written because facebook's $fb->api() function was very buggy. I'm not sure this is still needed. On the other hand, a future version of modules/fb might use this instead of faceobok's PHP SDK, eliminating the need for it entirely.

3 calls to fb_call_method()
fb_get_friends in ./fb.module
A convenience method for returning a list of facebook friends.
fb_get_groups_data in ./fb.module
fb_permission_form_user_profile_form_alter in contrib/fb_permission.module
Implements hook_form_FORM_ID_alter()

File

./fb.module, line 722
This is the core required module of Drupal for Facebook.

Code

function fb_call_method($fb, $method, $params = array()) {
  if (!isset($params['access_token'])) {
    $params['access_token'] = fb_get_token($fb);
  }
  $params['format'] = 'json-strings';

  // Here's how to create a url that conforms to standards:
  $url = url("https://api.facebook.com/method/{$method}", array(
    'query' => $params,
  ));

  // If facebook gives errors like "Invalid OAuth 2.0 Access Token 190/Unable to get application prop" it might be necessary to uncomment the urldecode below.
  // http://forum.developers.facebook.net/viewtopic.php?id=76228
  // $url = rawurldecode($url);
  $http = drupal_http_request($url);
  if (!isset($http->error) && isset($http->data)) {
    $data = json_decode($http->data, TRUE);

    // Yes, it's double encoded. At least sometimes.
    if (is_string($data)) {
      $data = json_decode($data, TRUE);
    }
    if (is_array($data)) {
      if (isset($data['error_code'])) {
        throw new FacebookApiException($data);
      }
    }
    elseif ($http->data == 'true' || $http->code == 200) {

      // No problems.
    }
    else {

      // Never reach this???
      if (function_exists('dpm')) {
        dpm($http, __FUNCTION__ . " unexpected result from {$url}");
      }

      // XXX
    }
    return $data;
  }
  else {

    // Should we throw FacebookApiException, or plain old exception?
    throw new FacebookApiException(array(
      'error_msg' => t('fb_call_method failed calling !method.  !detail', array(
        '!method' => $method,
        '!detail' => $http->error,
      )),
      'error_code' => $http->code,
    ));
  }
}