You are here

function amazon_item_clean_xml in Amazon Product Advertisement API 6

Same name and namespace in other branches
  1. 7.2 amazon.module \amazon_item_clean_xml()
  2. 7 amazon.module \amazon_item_clean_xml()

Take the Amazon XML item and turn it into our own private 'cleaned' data structure.

Parameters

$xml: XML structure as returned from Amazon API call.

Return value

'Cleaned' XML structure for local use.

3 calls to amazon_item_clean_xml()
amazon_search_simple_search in amazon_search/amazon_search.module
_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 448

Code

function amazon_item_clean_xml($xml) {
  $metadata = amazon_data_cache();
  $item = array();

  // Pull the absolute basic information Amazon keeps at the top level
  // of the XML tree, cast to string, and move on.
  $item['asin'] = (string) $xml->ASIN;
  if (!empty($xml->ItemAttributes->ISBN)) {
    $item['isbn'] = (string) $xml->ItemAttributes->ISBN;
  }
  if (!empty($xml->ItemAttributes->EAN)) {
    $item['ean'] = (string) $xml->ItemAttributes->EAN;
  }
  $item['salesrank'] = (string) $xml->SalesRank;
  $item['detailpageurl'] = (string) $xml->DetailPageURL;
  if (!empty($xml->ItemAttributes->ListPrice)) {
    $item['listpriceamount'] = (string) $xml->ItemAttributes->ListPrice->Amount;
    $item['listpricecurrencycode'] = (string) $xml->ItemAttributes->ListPrice->CurrencyCode;
    $item['listpriceformattedprice'] = (string) $xml->ItemAttributes->ListPrice->FormattedPrice;
  }
  if (!empty($xml->OfferSummary->LowestNewPrice)) {
    $item['lowestpriceamount'] = (string) $xml->OfferSummary->LowestNewPrice->Amount;
    $item['lowestpricecurrencycode'] = (string) $xml->OfferSummary->LowestNewPrice->CurrencyCode;
    $item['lowestpriceformattedprice'] = (string) $xml->OfferSummary->LowestNewPrice->FormattedPrice;
  }

  // Note that this one assumes we've searched with Merchant = Amazon.
  // Otherwise we can do an xpath search looking for the actual amazon listing.
  if (!empty($xml->Offers->Offer[0]->OfferListing->Price)) {
    $item['amazonpriceamount'] = intval($xml->Offers->Offer[0]->OfferListing->Price->Amount);
    $item['amazonpricecurrencycode'] = (string) $xml->Offers->Offer[0]->OfferListing->Price->CurrencyCode;
    $item['amazonpriceformattedprice'] = (string) $xml->Offers->Offer[0]->OfferListing->Price->FormattedPrice;
  }
  $participant_types = split(',', AMAZON_PARTICIPANT_TYPES);

  // Pull in the basics of the ItemAttributes collection.
  foreach ((array) $xml->ItemAttributes as $key => $value) {
    if (is_string($value) && !in_array($key, $participant_types)) {
      $key = strtolower($key);
      $item[$key] = $value;
    }
  }

  // Handle the Authors/Artists/Etc.
  foreach ($participant_types as $key) {
    if (isset($xml->ItemAttributes->{$key})) {
      foreach ($xml->ItemAttributes->{$key} as $value) {
        $item[strtolower($key)][] = (string) $value;
        $item['participants'][] = (string) $value;
      }
    }
  }

  // Handle the product images. In theory, there could be a million different
  // product image types. We're only going to check for the most common ones
  // and ignore the rest for now.
  $supported_sizes = split(',', AMAZON_IMAGE_SIZES);
  if (isset($xml->ImageSets->ImageSet)) {
    foreach ((array) $xml->ImageSets->ImageSet as $key => $data) {
      if (in_array($key, $supported_sizes)) {
        $item['imagesets'][strtolower($key)] = array(
          'url' => (string) $data->URL,
          'height' => (string) $data->Height,
          'width' => (string) $data->Width,
        );
      }
    }
  }

  // Handle the editorial reviews.
  if (isset($xml->EditorialReviews)) {
    foreach ($xml->EditorialReviews->EditorialReview as $data) {
      $item['editorialreviews'][] = array(
        'source' => (string) $data->Source,
        'content' => (string) $data->Content,
      );
    }
  }

  // And the customer reviews.
  if (isset($xml->CustomerReviews)) {
    $item['customerreviews_iframe'] = (string) $xml->CustomerReviews->IFrameURL;
  }

  // Give other modules an opportunity to pull out other bits of Amazon data
  // that would otherwise be ignored. We can't use module_invoke_all, as it
  // would lose the reference.
  foreach (module_implements('amazon_item_clean_xml') as $module) {
    $function = $module . '_amazon_item_clean_xml';
    $function($item, $xml);
  }
  return $item;
}