You are here

function fb_api in Drupal for Facebook 7.4

Same name and namespace in other branches
  1. 6.3 fb.module \fb_api()
  2. 7.3 fb.module \fb_api()

Facebook's older api methods.

1 call to fb_api()
fb_fql in ./fb.module

File

./fb.module, line 1443

Code

function fb_api($method, $params) {
  $params['format'] = 'json-strings';
  $app = fb_get_app();
  $params['api_key'] = $app['client_id'];
  $url = url("https://api.facebook.com/method/{$method}", array(
    'query' => $params,
  ));
  $http = drupal_http_request($url);
  if ($http->data) {
    $data = fb_json_decode($http->data);

    // Yes, it's double encoded. At least sometimes.
    if (is_string($data)) {
      $data = fb_json_decode($data);
    }
    if (is_array($data)) {
      if (isset($data['error_code'])) {
        throw new Exception($data['error_msg'], $data['error_code']);
      }
    }
    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,
    ));
  }
}