You are here

function fb_http_parse_response in Drupal for Facebook 7.4

Helper to parse data return from fb graph API.

Called from both fb_graph() and fb_graph_batch() which deal with slightly different data structures. Written to support both.

2 calls to fb_http_parse_response()
fb_graph_batch in ./fb.module
Read from facebook's graph in batch mode. Allows a single request to facebook to return data from multiple graph nodes.
fb_http in ./fb.module
Wrapper around drupal_http_request() which detects error codes.

File

./fb.module, line 1167

Code

function fb_http_parse_response($response) {
  if (is_object($response) && $response->data) {
    $data = fb_json_decode($response->data);

    // Yes, it's double encoded. At least sometimes.
    if (is_string($data)) {

      //dpm($data, __FUNCTION__ . " double encoded response!"); // Still happens?
      $data = fb_json_decode($data);
    }
  }
  elseif (is_array($response) && !empty($response['body'])) {

    // Data returned to fb_graph.
    $response = (object) $response;
    $data = fb_json_decode($response->body);
  }
  if (isset($response->error)) {
    $msg = t("!error: !detail (http !code)", array(
      '!error' => $response->error,
      '!code' => $response->code,
      '!detail' => implode(' ', $data['error']),
    ));
    throw new fb_GraphException($msg, $data['error']['code'], $response->code);
  }
  elseif (isset($data['error'])) {

    // Sometimes facebook response is OK, even though data is error.
    $msg = t("(!type !code) !error: !detail", array(
      '!error' => $data['error']['message'],
      '!code' => $data['error']['code'],
      '!type' => $data['error']['type'],
      '!detail' => implode(' ', $data['error']),
    ));
    throw new fb_GraphException($msg, $data['error']['code'], $response->code);
  }
  else {

    // Success.
    return $data;
  }
}