PHP and Amazon Associates
Introduction
Pre-Setup
After your registration to the amazon associates program you've got a key and an associates-id. These both values are required to work with the interface. Best solution is to store those values in your config.php (or whatever your configuration file is called) and declare them using the define()-function:define('AAS_KEY', 'xxxxxxxxxxxxxxxxxxxx');
define('AAS_ASSOCIATE', 'yourid-20');
That's all for now.
First Query
The only thing we need now is the ASIN of a product whose information we'd like to fetch. The following lines will fetch the xml document containing the information about a given ASIN (which is a books ISBN-10 for my example).// Array to store information about the book
$book = array();
// ASIN (ISBN-10) of the book
$asin = '0446676950';
// Builds the request URL
$request = 'http://ecs.amazonaws.co/onca/xml?Service=
'AWSECommerceService&'.
'AWSAccessKeyId='.AAS_KEY. // Your key
'&AssociateTag='.AAS_ASSOCIATE. // The associates-id
'&Operation=ItemLookup&ItemId='.$asin. // ASIN of the product.
'&ResponseGroup=Medium,Offers'; // List of what we'd like to get.
// Fetches the response of $request into a string
// (allow_url_fopen = true is required - check phpinfo())
$content = file_get_contents($request);
// Loads the string as a xml document into simpleXML
$xml = simplexml_load_string($content);
// Gets some major information about our product.
$book['title'] = $xml->Items->Item->ItemAttributes->Title;
$book['author'] = $xml->Items->Item->ItemAttributes->Author;
$book['price'] = $xml->Items->Item->Offers->Offer->OfferListing->Price->FormattedPrice;
$book['image'] = $xml->Items->Item->SmallImage->URL;
$book['link'] = $xml->Items->Item->DetailPageURL;
The above works for most cases. If you need more information about your product, just print out the xml document using var_dump($xml);. Now let's see what we've got: 1. The script automaticly builds the URL using a given ASIN - 2. We can fetch information about a product and work with it.
So far, so good. But what happens if your page gets called more than once? The script would fetch the xml document again and again. This will definetely lead to a lack of performance as well as to a rising traffic. The only way to avoid this is to build a cache.
Adding caching functionality
To add caching functionality we add the following two lines to our configuraton:// Must be writeable by the script
define('AAS_CACHE_DIR','/path/to/cache/dir/');
// Time to store the data (24h == 86400s)
define('AAS_CACHE_TIMEOUT', '86400');
The first line specifies the directory we will store the fetched xml data in, second line defines the amount of time until we fetch the data again. The cache directory, of course, has to be writeable by the script we are developing.
// Caching
$filename = AWS_CACHE_DIR . $asin . ".xml";
if(file_exists($filename)
AND filemtime($filename) > time() - AWS_CACHE_TIMEOUT)
{
$content = file_get_contents($filename);
}
else
{
if(file_exists($filename))
unlink($filename);
$request = "http://ecs.amazonaws.com
"/onca/xml?Service=AWSECommerceService".
"&AWSAccessKeyId=".AWS_KEY.
"&AssociateTag=".AWS_ASSOCIATE.
"&Operation=ItemLookup&ItemId=".$asin.
"&ResponseGroup=Medium,Offers";
$content = file_get_contents($request);
file_put_contents($filename, $content);
}
// Loads the string as a xml document into simpleXML
$xml = simplexml_load_string($content);
[..]
As the ASIN is unique, we can name our cache files by the ASINs we fetch. If a cache file for a to be loaded ASIN already exists we first check the time which has passed by since file creation. When a file is younger than our cache timeout we directly load the xml from the file, otherwise we unlink the file and refetch the data.
Recommended books on this topic
![]() |
Practical Web 2.0 Applications with PHP by Quentin Zervaas |
Comments (0) |
