You are here

function fb_graph in Drupal for Facebook 7.4

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

Read from facebook's graph.

Parameters

$do_cache: Flags indicating which cache settings to use.

22 calls to fb_graph()
fb_admin_application_edit_form_validate in ./fb.admin.inc
Form validation.
fb_admin_app_info in ./fb.admin.inc
Display detailed information about an application.
fb_admin_app_select_form in ./fb.admin.inc
fb_admin_app_select_form_validate in ./fb.admin.inc
fb_admin_long_lived_token in ./fb.admin.inc

... See full list

File

./fb.module, line 1228

Code

function fb_graph($path, $params = NULL, $do_cache = NULL) {
  if (isset($params['access_token']) && !$params['access_token'] && function_exists('debugger')) {
    debugger();
  }

  // Accept either params array or access token, for caller's convenience.
  if (is_array($params)) {
    $token = isset($params['access_token']) ? $params['access_token'] : NULL;
  }
  elseif (is_string($params)) {
    $token = $params;
    $params = array();
  }
  else {
    $token = NULL;
    $params = array();
  }
  if ($token === NULL) {
    $token = fb_access_token();
    if ($do_cache === NULL) {

      // Default cache setting, when no token passed in.
      $do_cache = variable_get(FB_VAR_USE_CACHE, FB_CACHE_QUERY | FB_CACHE_STORE);
    }
  }
  if ($do_cache != FB_CACHE_NONE) {
    $cid = $path;
  }

  // Cache paths starting with keywords in the session only.
  $session_cache = FALSE;
  foreach (array(
    'me',
    'app',
  ) as $key) {
    if (strpos($path, $key) === 0) {
      $session_cache = TRUE;
      $cid = FALSE;
      break;
    }
  }

  // Return app, me from session.
  if ($session_cache && ($do_cache && FB_CACHE_QUERY) && !empty($_SESSION) && !empty($_SESSION['fb']) && !empty($_SESSION['fb'][$token]) && !empty($_SESSION['fb'][$token][$path])) {
    return $_SESSION['fb'][$token][$path];
  }

  // Return other paths from cache.
  if ($do_cache & FB_CACHE_QUERY && $cid) {
    $cache = cache_get($cid, 'cache_fb');
    if ($cache && !empty($cache->data)) {
      return $cache->data;
    }
  }

  // Complex graph params must be json encoded.
  foreach ($params as $key => $value) {
    if (is_array($value)) {
      $params[$key] = json_encode($value);
    }
  }

  // Path not cached, query facebook graph.
  if ($token) {
    $params['access_token'] = $token;
  }
  $url = url("https://graph.facebook.com/{$path}", array(
    'query' => $params,
  ));
  $return = fb_http($url);

  // Cache results.
  if ($session_cache && $do_cache & FB_CACHE_STORE) {
    $_SESSION['fb'][$token][$path] = $return;
  }
  if (!empty($cid) && $do_cache & FB_CACHE_STORE) {
    cache_set($cid, $return, 'cache_fb', CACHE_TEMPORARY);
    if (!empty($return['id']) && $return['id'] != $cid) {
      cache_set($return['id'], $return, 'cache_fb', CACHE_TEMPORARY);
    }
  }
  return $return;
}