arcster.com

Home | Quotes  | Feedback  | Links | What I've been consuming | Code

3/1/10 - This code no longer works, as Amazon requires Authenticated REST queries

Using REST to do an ItemSearch Amazon ASIN query and transform the resulting XML into HTML.

Prerequisites:

  • Basic knowledge of HTML, XML, PHP
  • An Amazon developer ID
  • An Amazon associate ID - *5/7/09 update: Use AssociateTag, not associate-id, to get credit in the purchaseurl*
  • A web hosting server running PHP
  • An XML transformation tool (PHP 5 and development environments like PEAR and Zend have built-in tools for transforming XML. As one of the few people still running PHP 4 thanks to Yahoo, I'm using Macromedia's MM_XSLTransform.class.php.

Here is the query (substitute the bracketed phrases with your own ids)

<?php
require_once('includes/MM_XSLTransform/MM_XSLTransform.class.php');
$restquery = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService
&AWSAccessKeyId=[your access key]&Operation=ItemLookup&ResponseGroup=
Medium&AssociateTag=[your associate id]&ItemId=" . $asin;
$mm_xsl = new MM_XSLTransform();
$mm_xsl->setXML($restquery);
$mm_xsl->setXSL("aws1.xsl");
?>

You can pass the ASIN as a URL parameter or pull it from a database. Next I run the transform. The if condition displays a back button if the user is coming from a page in my domain

<?php
echo $mm_xsl->Transform();
if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], "arcster.com")) {
echo "<p><a href=\"" . $_SERVER['HTTP_REFERER'] . "\">Back</a></p>";
}
?>

Here's what the xsl file looks like. Thanks to KTS on stackoverflow who provided some much needed help with my transform

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml"
xmlns:tns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://webservices.amazon.com/AWSECommerceService/2005-10-05">
<xsl:output method="html"/>
<xsl:template match="/">
<table border="0">
<tr>
<td>
<h2>arcster.com - results from amazon.com</h2>
</td>
</tr>
<tr>
<td valign="top">
<xsl:apply-templates select="a:ItemLookupResponse/a:Items"/>
</td>
</tr>
</table>
</xsl:template>

<xsl:template match="a:Items">
<table border="0">
<xsl:apply-templates select="a:Item"/>
</table>
</xsl:template>

<xsl:template match="a:Item">
<tr>
<td>
<img>
<xsl:attribute name="src">
<xsl:value-of select="a:SmallImage/a:URL" />
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="a:ItemAttributes/a:Title" />
</xsl:attribute>
<xsl:attribute name="align">left</xsl:attribute>
</img>
<a>
<xsl:attribute name="href">
<xsl:value-of select="a:DetailPageURL" />
</xsl:attribute>
<xsl:value-of select="a:ItemAttributes/a:Title" />
</a>
<br/>
</td>
</tr>
<tr>
<td>
Price: <xsl:value-of select="a:ItemAttributes/a:ListPrice/a:FormattedPrice" /><br />
<xsl:if test="a:ItemAttributes/a:Artist">
Artist: <xsl:value-of select="a:ItemAttributes/a:Artist" />
</xsl:if>
<xsl:if test="a:ItemAttributes/a:Author">
Author: <xsl:value-of select="a:ItemAttributes/a:Author" />
</xsl:if>

</td>
</tr>
</xsl:template>
</xsl:stylesheet>

Random Link

hulu

If you see a box of pine
With a name that looks like mine
Just say I drowned in a barrel of wine
When I got to the border
- Richard Thompson