PHP script to optimize conversions on Amazon Associates

Within this article i will explain strategies how to increase the conversions of Amazon Associates partner program with the help of a self written PHP script that shows context sensitive book recommendations for specific blog posts. The script creates a REST based URL call to the Amazon SearchItem service in order to search for books for a given keyword. Then it extracts the book ids and adds the ids to a table of normal Amazon product link images, the screenshot below shows the resulting screen layout. It is a really simple script that helps to change your context-related book advertises more often in order to attract and interest your page visitors.

: a specific aStore is one possibility Amazon offers for promoting a collection of specific products. Its quite easy to integrate within your Web site but the conversion rate is quite low. It definitely depends on the kind of site where the aStore is integrated in. In my case i am writing a Blog, so visitors are expecting to read interesting articles and are less interested in shopping in a web store.Some initial thoughts about the different possibilities Amazon offers for advertising products on your Web site:

  • Banner Links: These links show specific ads created by Amazon, in order to promote a specific product, such as a new Kindle. It depends on the promoted product if visitors are going to click on a banner link.
  • Product Links: I got the best conversion rates with product links that are directly related to the article that i am writing. Furthermore, a direct link to a book i am writing about in an article is quite informative for a visitor and gives your article an added value.

The resulting table looks like shown in this screenshot below:So as a result to my observation that context-related product links are producing the highest conversion rates, i implemented a really simple script that encodes an Amazon request REST based URL in order to get related products back. My script then shows a table of four books that are related to the keywords given by a each blog post’s tag words. Of course i can only put a maximum of 2 keywords in my search, otherwise Amazon will always return no results at all.

My script works much better than any Amazon published third party library, and it depends on no other Amazon library except on PHP 5.

You can download and use my PHP script free without fee and without garantee 🙂

Download the PHP script here: recommendBooks

Here you can find the code:


$keyid = "yourkey";
$sec = "yoursecret";
$asid= "smar0dd-20"; // change to your asid
$alternativeKeywords = "Startup";


$timeStamp = gmdate("Y-m-d\TH:i:s\Z");

$posttags = get_the_tags($post->ID);
$count = 0; // we use only max 2 tags, otherwise Amazon does not return a search result
   $index = rand(0, sizeof($posttags) - 1);
   $count = 0;
   foreach($posttags as $this_tag) {
      if($count == index) {
	$tagdisplay = trim($tagdisplay) . ' ' . trim($this_tag->name);
else {
$tagdisplay = $alternativeKeywords;

$serviceAdr = "";

$aValues = array ('ResponseGroup' => 'Large', 'ItemPage' => '1', 'Sort' => 'salesrank', 'AssociateTag' => $asid, 'Timestamp' => $timeStamp,'Service' => 'AWSECommerceService', 'AWSAccessKeyId' => $keyid, 'Operation' => 'ItemSearch', 'SearchIndex' => 'Books', 'Keywords' => $tagdisplay, 'ResponseGroup' => 'ItemAttributes', 'Version' => '2009-01-06');
ksort ( $aValues );

$string = implode('&', array_map(function($key, $val) {
return '' . rawurlencode($key) . '=' . rawurlencode($val);
array_keys($aValues), $aValues)

$encString = "GET\\n/onca/xml\n" . $string;

$signature = base64_encode(hash_hmac("sha256", $encString, $sec, True));

$call = $serviceAdr . '?' . $string . '&Signature=' . rawurlencode($signature);

$r = file_get_contents($call);

// now start to parse xml response
$asins = explode("<ASIN>", $r);

// select one asin out of the ten from the first result page
$asin = substr($asins[rand(1,10)], 0, 10);
$asin2 = substr($asins[rand(1,10)], 0, 10);
$asin3 = substr($asins[rand(1,10)], 0, 10);
$asin4 = substr($asins[rand(1,10)], 0, 10);
catch(Exception $e)
echo $e->getMessage();
<table width="100%">
<th >
<a href="<?php print($asin); ?>/ref=as_li_tf_il?ie=UTF8&camp=1789&creative=9325&creativeASIN=<?php print($asin); ?>&linkCode=as2&tag=<?php print($asid); ?>"><img border="0" src="<?php print($asin); ?>&Format=_SL160_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=<?php print($asid); ?>" ></a><img src="<?php print($asid); ?>&l=as2&o=1&a=<?php print($asin); ?>" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />
<a href="<?php print($asin2); ?>/ref=as_li_tf_il?ie=UTF8&camp=1789&creative=9325&creativeASIN=<?php print($asin2); ?>&linkCode=as2&tag=<?php print($asid); ?>"><img border="0" src="<?php print($asin2); ?>&Format=_SL160_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=<?php print($asid); ?>" ></a><img src="<?php print($asid); ?>&l=as2&o=1&a=<?php print($asin2); ?>" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />
<a href="<?php print($asin3); ?>/ref=as_li_tf_il?ie=UTF8&camp=1789&creative=9325&creativeASIN=<?php print($asin3); ?>&linkCode=as2&tag=<?php print($asid); ?>"><img border="0" src="<?php print($asin3); ?>&Format=_SL160_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=<?php print($asid); ?>" ></a><img src="<?php print($asid); ?>&l=as2&o=1&a=<?php print($asin3); ?>" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />
<a href=""><img border="0" src="" /></a>

Leave a Reply

Your email address will not be published. Required fields are marked *