You are here

function flickr_request in Flickr 5

Same name and namespace in other branches
  1. 6 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 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.

11 calls to flickr_request()
flickr_people_get_info in ./flickr.inc
flickr_photoset_get_info in ./flickr.inc
flickr_photoset_get_list in ./flickr.inc
flickr_photos_search in ./flickr.inc
flickr_photo_get_info in ./flickr.inc

... See full list

File

./flickr.inc, line 55

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',
        //In Drupal <= 5.1, only HTTP errors are stored in $result->code correctly, not TCP/IP errors.

        //We can not count on this variable being correct until this module requires Drupal 5.2 or above.
        '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}", 'cache', $result->data, time() + variable_get('flickr_cache_duration', 3600));
  }
  return $response;
}