Resources for Developers Using Amazon's Product Advertising API
· Home  
· Search  
· Browse Nodes  
· Data Feed?  
· FAQs  
· One-Second Rule  
· PHP Examples  
· Tips for Associates  
· Tools  
· Understanding A2S  

Create a custom Amazon Associate Store in minutes with Associate-O-Matic.

PHP A2S Demo 1 - ItemLookup

The following is a simple example of using PHP to perform an A2S ItemLookup operation and display the information in HTML. It uses functions usually included in PHP version 4 and later.


<?php

//***********************************************
// PHP A2S Demo 1
//
// A demonstration of one way of receiving and
// displaying information from Amazon's A2S using
// PHP. This demonstrates using A2S's ItemLookup
// operation. The results are parsed using
// the XML functions available in PHP version 4
// and later.
//
// Roger Smith
// a2sdeveloper.com
//***********************************************

//***********************************************
// A method for getting the XML results from
// Amazon.com.  This is based on the code at:
// http://www.a2sdeveloper.com/
//   page-using-curl-to-get-results-from-ecs.html
// If cURL is not available, this method uses
// the file function to get the results. This
// assumes that fopen wrappers have been enabled.
//***********************************************
function getAmazonPage($url, &$code, &$headers) 
 { 
  if (function_exists('curl_exec'))
    { 
     $ch = curl_init(); 
     curl_setopt ($ch, CURLOPT_URL, $url); 
     curl_setopt ($ch, CURLOPT_USERAGENT, ' Your Name /1.0 (+ http://www. yourdomain.com /)'); 
     curl_setopt ($ch, CURLOPT_HEADER, 1); 
     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt ($ch, CURLOPT_TIMEOUT, 120); 
     $result = curl_exec ($ch); 
     $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
     $hsize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 
     $headers = substr($result, 0, $hsize - 2); 
     $result = substr($result, $hsize); 
     curl_close($ch); 
     return $result;
    }
  $code = 200;
  $headers = "";
  $result = implode('', file($url));
 } 


// Change this to your AWS Access Key
$key = "144RR0T0YM45X6SVKSR2";

// Change this to your Associates Tag if you
// have one
$tag = "oblio-20";

// Change this to the ASIN of the Product to Display
$ASIN = "B0001JXPSO";

// This is the version of A2S we will use. This is
// important because each new release of A2S may
// change the information returned.
$version = "2007-07-16";

// Display the start of the HTML web page. I'm
// using the "heredoc" method, so make sure the
// line "HEADER;" starts in the first column
// of the file.
echo <<<HEADER
<html>
<head>
<title>PHP A2S Demo 1</title>
</head>
<body>
HEADER;


// First we create the URL of the REST query.
$url = "http://ecs.amazonaws.com/onca/xml" .
       "?Service=AWSECommerceService" .
       "&Version=$version" .
       "&Operation=ItemLookup" .
       "&SubscriptionId=$key" .
       "&AssociateTag=$tag" .
       "&ItemId=$ASIN" .
       "&IdType=ASIN" .
       "&ResponseGroup=Large";

// Next we get the results from A2S
$results = getAmazonPage($url, $code, $headers);

if ($code == 500 || $code == 503)
  {
   echo "Error getting XML from Amazon.com";
  }
 else
  {
   // Create a parser for the XML
   $parser = xml_parser_create();
   xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING,0);
   xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE,1);
   xml_parse_into_struct($parser, $results, $values, $tags);
   xml_parser_free($parser);

   // We now have the XML information in an array
   // called $values.  We loop through the array
   // to process the values.


   $inItemAttributes = false;
   $inMediumImage = false;
   $inEditorialReview = false;
   $itemAttributes = array();
   foreach ($values as $key=>$val) 
     {
      switch ($val['tag'])
       {
        case 'DetailPageURL':
          $DetailPageURL = $val['value'];
          break;
        case 'ItemAttributes':
          $inItemAttributes = ($val['type'] == 'open');
          break;
        case 'MediumImage':
          $inMediumImage = ($val['type'] == 'open');
          break;
        case 'EditorialReview':
          $inEditorialReview = ($val['type'] == 'open');
          break;
        case 'Content':
          if ($inEditorialReview) $EditorialReview = $val['value'];
          break;
        case 'URL':
          if ($inMediumImage) $MediumImage = $val['value'];
          break;
        default:
          if ($inItemAttributes)
            {
             $itemAttributes[$val['tag']] = $val['value'];
            }
       }
     }

   $Title = $itemAttributes['Title'];

   echo "<h1><a href=\"$DetailPageURL\">$Title</a></h1>";

   echo "<img src=\"$MediumImage\" align=\"right\">";

   echo $EditorialReview;   

  }

// Display the end of the HTML web page. I'm
// using the "heredoc" method, so make sure the
// line "TRAILER;" starts in the first column
// of the file.
echo <<<TRAILER
<p>
PHP A2S Demo 1 - See: <a href="http://www.a2sdeveloper.com/page-php-examples.html">http://www.a2sdeveloper.com/page-php-examples.html</a>
</body>
TRAILER;

?>

View the output of the above code.


Copyright © 2010 by Roger Smith