You are here

function flickr_request in Flickr 6

Same name and namespace in other branches
  1. 5 flickr.inc \flickr_request()
  2. 7 flickr.inc \flickr_request()

Submit a request to Flickr.

Parameters

$method: string method name

$args: associative array of arguments names and values

$cacheable: boolean indicating if it's safe to cache the results of this request

$return_errors: boolean indicating if the caller will handle displaying error messages

Return value

an array with the the result of the request, or FALSE on error.

17 calls to flickr_request()
flickr_favorites_get_public_list in ./flickr.api.inc
Returns a list of favorite public photos for the given user.
flickr_get_group_photos in ./flickr.api.inc
flickr_people_get_info in ./flickr.api.inc
flickr_photoset_get_info in ./flickr.api.inc
flickr_photoset_get_list in ./flickr.api.inc

... See full list

File

./flickr.inc, line 21

Code

function flickr_request($method, $args, $cacheable = TRUE, $return_errors = FALSE) {

  // Add in additional parameters then sort them for signing.
  $args['api_key'] = trim(variable_get('flickr_api_key', ''));
  $args['method'] = $method;
  $args['format'] = 'php_serial';
  ksort($args);

  // Build an argument hash API signing (we'll also use it for the cache id).
  $arg_hash = '';
  foreach ($args as $k => $v) {
    $arg_hash .= $k . $v;
  }

  // If we've got a secret, sign the arguments.
  if ($secret = trim(variable_get('flickr_api_secret', ''))) {
    $args['api_sig'] = md5($secret . $arg_hash);
  }

  // Build the URL.
  foreach ($args as $k => $v) {
    $encoded_params[] = urlencode($k) . '=' . urlencode($v);
  }
  $url = FLICKR_REST_ENDPOINT . '?' . implode('&', $encoded_params);

  // If it's a cachable request, try to load a cached value.
  if ($cacheable) {
    if ($cache = cache_get("flickr_{$arg_hash}", 'cache')) {

      // Check that the value is still "fresh".
      if ($cache->expire > time()) {
        return unserialize($cache->data);
      }
    }
  }

  // If a cached value wasn't suitable, attempt to connect and fetch a result.
  $result = drupal_http_request($url);
  if ($result->code != 200) {
    if ($return_errors) {
      return array(
        'stat' => 'error',
        'code' => $result->code,
        'message' => $result->error,
      );
    }
    flickr_set_error(t("Could not connect to Flickr, Error: @error", array(
      '@error' => $result->error,
    )));
    return FALSE;
  }

  // Make sure it unserializes.
  $response = unserialize($result->data);
  if (!$response) {
    if ($return_errors) {
      return array(
        'stat' => 'error',
        'code' => '-1',
        'message' => 'The response was corrupted, it could not be unserialized.',
      );
    }
    flickr_set_error(t("Flickr's response was corrupted and could not be unserialized."));
    return FALSE;
  }

  // Check that the request was successful.
  if (flickr_response_has_error($response)) {
    if ($return_errors) {
      return $response;
    }
    flickr_set_error($response);
    return FALSE;
  }

  // Save cacheable results for future use.
  if ($cacheable) {
    cache_set("flickr_{$arg_hash}", $result->data, 'cache', time() + variable_get('flickr_cache_duration', 3600));
  }
  return $response;
}