You are here

function amazon_http_request in Amazon Product Advertisement API 6

Same name and namespace in other branches
  1. 7.2 amazon.module \amazon_http_request()
  2. 7 amazon.module \amazon_http_request()
5 calls to amazon_http_request()
amazon_ean_to_asin in ./amazon.module
Given an EAN (ISBN-13), try to get Amazon to give it to us.
amazon_search_simple_search in amazon_search/amazon_search.module
asin_autocomplete_callback in asin/asin.module
Autocomplete callback for the asin_autocomplete widget.
_amazon_item_batch_lookup_from_web in ./amazon.module
Get 10 or less items from the AWS web service. AWS allows ONLY 10 items, See http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.ht....
_asin_devel_generate in asin/asin.module
Utility function that actually provides the values for asin_devel_generate().

File

./amazon.module, line 216

Code

function amazon_http_request($operation, $parameters = array(), $locale = NULL) {
  if (!isset($locale)) {
    $locale = variable_get('amazon_locale', 'US');
  }
  $metadata = amazon_data_cache();
  $locale_data = $metadata['locales'][$locale];

  // Populate the params with default data.
  $parameters += array(
    'Service' => 'AWSECommerceService',
    'Version' => AMAZON_ECS_SCHEMA,
    'AWSAccessKeyId' => variable_get('amazon_aws_access_key', ''),
    'Operation' => $operation,
  );
  if ($associate_id = amazon_get_associate_id()) {
    $parameters += array(
      'AssociateTag' => $associate_id,
    );
  }
  $parameters_after_hook = module_invoke_all('amazon_request', $parameters);
  if (!empty($parameters_after_hook)) {
    $parameters = $parameters_after_hook;
  }
  $parameters += array(
    'Timestamp' => gmdate("Y-m-d\\TH:i:s") . 'Z',
  );
  uksort($parameters, 'strnatcmp');
  $params = array();
  foreach ($parameters as $key => $value) {
    if (is_array($value)) {
      $value = implode(',', $value);
    }
    $param = str_replace("%7E", "~", rawurlencode($key));
    $value = str_replace("%7E", "~", rawurlencode($value));
    $params[] = $param . '=' . $value;
  }
  $secret_access_key = variable_get('amazon_aws_secret_access_key', "");
  if ($secret_access_key == "") {
    watchdog('amazon', "No Secret Access Key configured. You must configure one at Admin->Settings->Amazon API", NULL, WATCHDOG_ERROR);
    drupal_set_message(t("Amazon Module: No Secret Access Key is configured. Please contact your site administrator"));
    return FALSE;
  }

  // Thanks for signature creation code from http://mierendo.com/software/aws_signed_query/
  $query_string = implode('&', $params);
  $parsed_url = parse_url($locale_data['url']);
  $host = strtolower($parsed_url['host']);
  $string_to_sign = "GET\n{$host}\n{$parsed_url['path']}\n{$query_string}";
  $signature = base64_encode(hash_hmac('sha256', $string_to_sign, $secret_access_key, TRUE));
  $signature = str_replace("%7E", "~", rawurlencode($signature));
  $query_string .= "&Signature={$signature}";
  $url = $locale_data['url'] . '?' . $query_string;

  // Make the request and return a SimpleXML object.
  $results = drupal_http_request($url, array(), 'GET');
  if ($results->code == 200) {
    $xml = new SimpleXMLElement($results->data);
    return $xml;
  }
  if ($results->code >= 400 && $results->code < 500) {
    try {
      $xml = new SimpleXMLElement($results->data);
    } catch (Exception $e) {
      watchdog('amazon', "Error handling results: http_code=%http_code, data=%data.", array(
        '%http_code' => $results->code,
        '%data' => (string) $results->data,
      ));
      return FALSE;
    }
    watchdog('amazon', "HTTP code %http_code accessing Amazon's AWS service: %code, %message", array(
      '%http_code' => $results->code,
      '%code' => (string) $xml->Error->Code,
      '%message' => (string) $xml->Error->Message,
    ));
    return FALSE;
  }
  watchdog('amazon', "Error accessing Amazon AWS web service with query '%url'. HTTP result code=%code, error=%error", array(
    '%code' => $results->code,
    '%error' => $results->error,
    '%url' => $url,
  ));
  return FALSE;
}