Web This Site

CaRP: Caching RSS Parser - Documentation


CaRP Interactive FAQ
Getting Started: Free Download | Purchase | Install
Reference: Functions | Plugins | Themes | Full Index
Etc.: Display Formatting | Example Code | Affiliates

Amazon.com Associates Example

Amazon.com associates, use this code to easy display best sellers based on whatever search terms you want The results are cached and refreshed every 12 hours, speeding the performance of your website. Once you've finished the main setup, adding custom best seller lists all over your website will be a snap!

This example was updated on 20 March 2008 to work with the most recent changes to Amazon's web services API.
For another method of generating affiliates RSS feeds, check out Grouper Evolution. It includes plugins supporting Amazon.com, Shopping.com and All Posters' affiliate programs. [Affiliates RSS Example]

First, copy the XSL template from below and save it on your web server with the filename "Amazon2RSS-2.0.xsl".

Second, copy the following code and save it in a file named "Amazon2RSS-2.0.php". Make the following changes, and then upload the file to the same directory as carp.php:

  • Change the URL in the variable $xsl to point to the XSL template saved in the previous step
  • Change put_your_access_key_id_here to your Amazon Access Key ID. Note the following from Amazon's documentation:

    "If you are new to ECS, you must register to become an ECS 4.0 developer. To register, point your browser at http://aws.amazon.com and click the Register for AWS link. Once you have registered, you will be assigned an Access Key ID.

    "If you are already an ECS 4.0 developer and have a Subscription ID, you will be assigned an Access Key ID automatically. To view your Access Key ID, point your browser at http://aws.amazon.com and click the Your Web Services Account button and pull down to the View Access Key Identifiers option. Your Access Key ID and Secret Access Key display."
  • Change put_your_associates_id_here to your Amazon associates ID. If you do not have an associates ID, sign up for one from Amazon.com.
  • If you wish to search a non-US Amazon site, change $county to the two letter code for the desired country.
<?php
$xslt='http://www.yourdomain.com/xsl/Amazon2RSS-2.0.xsl';
$AccessKeyId='put_your_access_key_id_here';
$AssociateTag='put_your_associates_id_here';
$country='US';

$countries=array(
	'CA'=>'ecs.amazonaws.ca',
	'DE'=>'ecs.amazonaws.de',
	'FR'=>'ecs.amazonaws.fr',
	'JP'=>'ecs.amazonaws.jp',
	'UK'=>'ecs.amazonaws.co.uk',
	'US'=>'ecs.amazonaws.com'
);

if (!($MaxItems+=0)) $MaxItems=5;
if (!($OneReview+=0)) $OneReview=1;
$MaxReviewLength+=0;
if (!isset($SearchIndex)) $SearchIndex='Books';

require_once dirname(__FILE__).'/carp.php';
CarpConf('cborder','');
CarpConf('iorder','image,link,author,desc');
CarpConf('cacheinterval',720);
CarpConf('maxitems',$MaxItems);
CarpConf('linktarget',1);
CarpConf('aidesc','');
CarpConf('ai','<br clear="all" /> <br />');
CarpConf('biimage','<div style="float:left;margin-right:5px;">');
CarpConf('aiimage','</div>');
$query='http://'.$countries[$country].'/onca/xml?Service=AWSECommerceService'.
	'&Operation=ItemSearch&ResponseGroup=Small,Reviews,Images,Offers'.
	"&AWSAccessKeyId=$AccessKeyId&AssociateTag=$AssociateTag".
	"&Sort=salesrank&Style=".urlencode($xslt)."&Keywords=".urlencode($q).
	"&SearchIndex=".urlencode($SearchIndex).
	($OneReview?'&CaRPOneReview=1':'').
	($MaxReviewLength?"&CaRPMaxReviewLength=$MaxReviewLength":'');
if (0) echo "<p>$query</p>";
CarpCacheShow($query);
?>

Third, find an Amazon.com logo you like and upload a copy of it to your website.

Finally, copy the following code (which will be a template for adding Amazon links to your web pages) and save it on your computer, changing:

  • the URL of the image to point to the one uploaded to your site in the previous step.
  • the associates ID in the image link (where it says "tag=moulc") to your ID.
  • the path to Amazon2RSS-2.0.php (the file created in the second step).
<div style="width:200;height:420px;overflow:auto;margin:5px;padding:5px;background:#cccccc;">
<span style="color:#003366;font-size:16pt;font-weight:bold;">Best sellers from</span><br>
<a href="http://www.amazon.com/exec/obidos/redirect?tag=moulc&path=subst/home/home.html"><img src="http://www.yourwebsite.com/img/126X32-w-logo.gif" border="0"></a><br>
<?php
$q='web design';
$MaxItems=5;
$OneReview=1;
$MaxReviewLength=250;
$SearchIndex='Books';
include '/path/to/Amazon2RSS-2.0.php';
?>
</div>

You've completed the main setup. Now you can add Amazon links to any page by pasting a copy of the code saved in the last step above into your web page with the following changes as needed:

  • the styles of the div and span to look the way you want them to (If you don't want everything displayed in a scrolling box, remove the opening and closing div tags completely).
  • the value of $q to the search terms you wish to use.
  • the value of $MaxItems to the maximum number of items you wish to display.
  • the value of $OneReview to 0 if you wish to display as many customer reviews as Amazon gives you in response to the search query (generally 3, if that many are available). The default setting of $OneReview=1 displays one review.
  • the value of $MaxReviewLength to the maximum number of characters you want displayed from each review (0 = display the entire review).
  • the value of $SearchIndex to the appropriate value for whatever product category you wish to search, if not books. [As of this writing, the allowed values, some of which are not available in all countries, are as follows (refer to Amazon's documentation for details): Apparel, Baby, Beauty, Blended, Books, Classical, DigitalMusic, DVD, Electronics, ForeignBooks, GourmetFood, HealthPersonalCare, HomeGarden, Jewelry, Kitchen, Magazines, Merchants, Miscellaneous, Music, MusicalInstruments, MusicTracks, OfficeProducts, OutdoorLiving, PCHardware, PetSupplies, Photo, Restaurants, Software, SoftwareVideoGames, SportingGoods, Tools, Toys, VHS, Video, VideoGames, Wireless, WirelessAccessories.]

That's it! Depending on whether you are using the free or commercial version of CaRP, something like the examples to the right will appear on your web page.

TROUBLESHOOTING: If you follow the above steps and no items are displayed:

  1. Change the "if (0)" to "if (1)" in Amazon2RSS-2.0.php.
  2. Copy the URL that's displayed and save it somewhere.
  3. Change "if (1)" to "if (0)" and upload the file again to turn off display of the URL.
  4. Load the URL that was displayed in your web browser.

If what is displayed is in RSS format, then the XSL part of the system is working, and you need to figure out why CaRP isn't displaying it. More likely, it won't be in RSS format, but will be in Amazon's XML format. Hopefully there will be an error message in it somewhere telling you what went wrong. If not, things to check include:

  • Do your associate ID and access key ID appear in the URL? If not, fix those in Amazon2RSS-2.0.php.
  • Find the URL within the URL which points to your XSL file (Style=...). Copy the URL and paste it into your web browser. Does the XSL template get displayed? If not, fix the URL in Amazon2RSS-2.0.php.

XSL template:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Raymond Yee (yee@uclink.berkeley.edu) , Interactive University, University of California, Berkeley 2002-07-26
Adapted for RSS 2.0, ECS 4.0, made configurable, etc. by Antone Roundy 2005-10-26
 
Raymond Yee  yee@uclink.berkeley.edu   Interactive University Project Amazon Lite to RSS 0.92 XSLT

Copyright (c) 2002 The Regents of the University of California
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this XSLT
style sheet for any purpose, provided that the above copyright notice
and the following two paragraphs appear in all copies of this
document.

IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS DOCUMENT TYPE DEFINITION, EVEN IF THE
UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE COMPUTER
FILE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE
UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.  

&nbsp; trick from http://www.mycgiserver.com/~~lisali/papers/paper.htm
-->
<!DOCTYPE stylesheet [
	<!ENTITY nbsp "<xsl:text disable-output-escaping=&#34;yes&#34; 
    xmlns:xsl= &#34;http://www.w3.org/1999/XSL/Transform &#34;
    >&amp;nbsp;</xsl:text>">
]>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:fo="http://www.w3.org/1999/XSL/Format"
	xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2005-10-05"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
>
<xsl:output encoding="UTF-8" method="xml" media-type="text/xml"/>
<xsl:template match="aws:ItemSearchResponse">
<xsl:variable name="CaRPOneReview">
	<xsl:choose>
		<xsl:when test="aws:OperationRequest/aws:Arguments/aws:Argument[@Name = 'CaRPOneReview']/@Value">
			<xsl:value-of select="aws:OperationRequest/aws:Arguments/aws:Argument[@Name = 'CaRPOneReview']/@Value" />
		</xsl:when>
		<xsl:otherwise><xsl:text>0</xsl:text></xsl:otherwise>
	</xsl:choose>
</xsl:variable>
<xsl:variable name="CaRPMaxReviewLength">
	<xsl:choose>
		<xsl:when test="aws:OperationRequest/aws:Arguments/aws:Argument[@Name = 'CaRPMaxReviewLength']/@Value">
			<xsl:value-of select="aws:OperationRequest/aws:Arguments/aws:Argument[@Name = 'CaRPMaxReviewLength']/@Value" />
		</xsl:when>
		<xsl:otherwise><xsl:text>250</xsl:text></xsl:otherwise>
	</xsl:choose>
</xsl:variable>
<rss version="2.0">
<channel>
	<title>Amazon Search Results</title>
	<link>http://www.amazon.com/</link>
	<description>Amazon Search Results</description>
	<xsl:apply-templates select="aws:Items/aws:Item">
		<xsl:with-param name="CaRPOneReview" select="$CaRPOneReview" />
		<xsl:with-param name="CaRPMaxReviewLength" select="$CaRPMaxReviewLength" />
	</xsl:apply-templates>
</channel>
</rss>
</xsl:template>

<xsl:template match="aws:Items/aws:Item">
<xsl:param name="CaRPOneReview" select="''"/>
<xsl:param name="CaRPMaxReviewLength" select="''"/>
<item>
<title><xsl:value-of select="aws:ItemAttributes/aws:Title"/></title>
<link><xsl:call-template name="fixurl">
<xsl:with-param name="string" select="aws:DetailPageURL"/></xsl:call-template></link>
<dc:creator><xsl:for-each select="aws:ItemAttributes/aws:Author">
	<xsl:apply-templates/>
	<xsl:if test="not(position()=last())">, </xsl:if>
</xsl:for-each></dc:creator>
<description>
<xsl:choose>
	<xsl:when test="$CaRPOneReview = '1'">
		&lt;b&gt;Amazon Price:&lt;/b&gt; <xsl:value-of select="aws:Offers/aws:Offer[1]/aws:OfferListing[1]/aws:Price/aws:FormattedPrice"/>
		<xsl:apply-templates select="aws:CustomerReviews/aws:Review[1]">
			<xsl:with-param name="CaRPOneReview" select="$CaRPOneReview" />
			<xsl:with-param name="CaRPMaxReviewLength" select="$CaRPMaxReviewLength" />
		</xsl:apply-templates>
	</xsl:when>
	<xsl:otherwise>
		&lt;b&gt;Amazon Price:&lt;/b&gt; <xsl:value-of select="aws:Offers/aws:Offer[1]/aws:OfferListing[1]/aws:Price/aws:FormattedPrice"/>&lt;br /&gt;
		&lt;b&gt;Customer Reviews:&lt;/b&gt;
		<xsl:apply-templates select="aws:CustomerReviews/aws:Review">
			<xsl:with-param name="CaRPOneReview" select="$CaRPOneReview" />
			<xsl:with-param name="CaRPMaxReviewLength" select="$CaRPMaxReviewLength" />
		</xsl:apply-templates>
	</xsl:otherwise>
</xsl:choose>
</description>
<!-- length="1000" is not accurate, but what can we do? we don't know the size of the file -->
<xsl:choose>
	<xsl:when test="contains(aws:SmallImage/aws:URL,'.gif')"><enclosure url="{aws:SmallImage/aws:URL}" length="1000" type="image/gif" /></xsl:when>
	<xsl:when test="contains(aws:SmallImage/aws:URL,'.jpeg') or contains(aws:SmallImage/aws:URL,'.jpg')"><enclosure url="{aws:SmallImage/aws:URL}" length="1000" type="image/jpeg" /></xsl:when>
	<xsl:when test="contains(aws:SmallImage/aws:URL,'.png')"><enclosure url="{aws:SmallImage/aws:URL}" length="1000" type="image/png" /></xsl:when>
</xsl:choose>
</item>
</xsl:template>

<xsl:template match="aws:CustomerReviews/aws:Review">
<xsl:param name="CaRPOneReview" select="''"/>
<xsl:param name="CaRPMaxReviewLength" select="''"/>
<xsl:choose>
	<xsl:when test="$CaRPOneReview = '1'">
		&lt;br/&gt;
		&lt;b&gt;Customer Review:&lt;/b&gt; <xsl:value-of select="substring(aws:Content,1,$CaRPMaxReviewLength)"/><xsl:if test="string-length(aws:Content)>$CaRPMaxReviewLength">...</xsl:if>
	</xsl:when>
	<xsl:otherwise>
		&lt;br/&gt;&amp;raquo; <xsl:value-of select="substring(aws:Content,1,$CaRPMaxReviewLength)"/><xsl:if test="string-length(aws:Content)>$CaRPMaxReviewLength">...</xsl:if>
		<xsl:if test="not(position()=last())">&lt;br /&gt;</xsl:if>
	</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="fixurl">
	<xsl:param name="string" select="''"/>
	<xsl:choose>
		<xsl:when test="contains($string,'%26')">
			<xsl:value-of select="substring-before($string,'%26')"/>&amp;<xsl:call-template name="fixurl">
			<xsl:with-param name="string" select="substring-after($string,'%26')"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$string"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>
Depending on whether you are using the free or commercial version of CaRP, the output will look similar to the following:

CaRP commercial version:

Best sellers from

Professional ASP.NET 3.5: In C# and VB (Programmer to Programmer)
by Bill Evjen, Scott Hanselman, Devin Rader
Amazon Price: $34.64
Customer Review: Being a Microsoft fanatic I always want to stay on top of things with Microsoft Technologies.I have ...
 
Don't Make Me Think: A Common Sense Approach to Web Usability, 2nd Edition
by Steve Krug
Amazon Price: $26.40
Customer Review: I am a web UI developer and I have bought a lot of books already including (theory and implementatio...
 
Dreamweaver CS3: The Missing Manual
by David McFarland
Amazon Price: $29.69
Customer Review: I painful dug thru many of the "well respected" writing out there and none of them measure up to th...
 
Influencer: The Power to Change Anything
by Kerry Patterson, Joseph Grenny, David Maxfield, Ron McMillan, Al Switzler
Amazon Price: $16.47
Customer Review: A great "how to" book that should be required reading for anyone in management. We've used a couple ...
 
Web Analytics: An Hour a Day
by Avinash Kaushik
Amazon Price: $19.79
Customer Review: If you find yourself stumped with how to improve your website, or don't know where to begin with loo...
 

CaRP free version:

Best sellers from

Professional ASP.NET 3.5: In C# and VB (Programmer to Programmer)
Amazon Price: $34.64
Customer Review: Being a Microsoft fanatic I always want to stay on top of things with Microsoft Technologies.I have ...
 
Don't Make Me Think: A Common Sense Approach to Web Usability, 2nd Edition
Amazon Price: $26.40
Customer Review: I am a web UI developer and I have bought a lot of books already including (theory and implementatio...
 
Dreamweaver CS3: The Missing Manual
Amazon Price: $29.69
Customer Review: I painful dug thru many of the "well respected" writing out there and none of them measure up to th...
 
Influencer: The Power to Change Anything
Amazon Price: $16.47
Customer Review: A great "how to" book that should be required reading for anyone in management. We've used a couple ...
 
Web Analytics: An Hour a Day
Amazon Price: $19.79
Customer Review: If you find yourself stumped with how to improve your website, or don't know where to begin with loo...