Support forum login

©2006-2010 IAAI Software

Contact Us

Amazon API

Submitted by dmorison on Fri, 2009-07-24 15:01.

Hi everyone,

As a very popular addition to Price Tapestry, and with the requirement for Amazon Web Services (AWS) API requests to be authenticated by 15th August 2009, i've updated the script and consolidated the various Amazon related threads into this single page to make it much easier for new users to find the relevant info.

If you are a member of the Amazon Associate Program (.co.uk / .com (US) / .de / .jp) you can complement your Price Tapestry generated product pages and/or search results with live Amazon results via Amazon Web Services. To use the web services, in addition to an associate account you will also need an AWS account.

Download and Installation

Click here to download, and unzip amazon.php to your local computer.

Open the file, and edit the following lines at the top of the script to contain your AWS access key ID, secret access key, and your associate tag where indicated. When logged in to AWS, your access key ID and secret access key can be found on this page.

  $amazonAWSAccessKeyId = "YOUR_ACCESS_KEY_ID";
  $amazonSecretAccessKey = "YOUR_SECRET_ACCESS_KEY";
  $amazonAssociateTag = "YOUR_ASSOCIATE_TAG";

To install, simply upload the modified amazon.php to your main Price Tapestry installation folder (probably the top level folder of your site unless you have installed Price Tapestry into a sub-directory).

Usage

Amazon results can be added to both search.php and products.php. In both files, to include Amazon results at the end of the page, look for the the following code towards the end of the script:

  require("html/footer.php");

...and REPLACE this with:

  require("amazon.php");
  require("html/footer.php");

Customisation for non-UK Locales

If you are not an amazon.co.uk associate you will need to edit line 122 as appropriate, for example by replacing .co.uk with .com (US associates).

Customisation for Niche Sites

One of the parameters in the AWS request generated by the script is "SearchIndex". A value of "Blended" is used by default, which searches all categories. If you are using the script on a niche site, it may be worthwhile using a more appropriate value based on the subject matter of your site. A complete list of SearchIndex values available per locale can be found here. The SearchIndex value can be changed on line 128 of the script.

IMPORTANT: At time of writing, the AWS ECommerce Service is free of charge, but make sure that you are checking the email address associated with your AWS account in case this situation changes in the future.

Submitted by jmsobe on Thu, 2009-08-20 13:59.

Thanks for releasing this. When I search for products I am only seeing 4 items from amazon. At the top of the search results screen it says that no items have been found oddly enough. Any ideas?

Submitted by dmorison on Thu, 2009-08-20 14:18.

Hi,

Regarding the number of Amazon results; this is limited to 4 in the script, but you can change this to whatever value you wish by modifying the following code on line 7 of amazon.php:

  $amazonItemMax = 4;

As Amazon results are API based and not from the local database, it is certainly possible that a query might not return any local results (I presume that search is working correctly for products that are in your database); but for there to be results from Amazon.

What may be worth doing because of this; would be to add some kind of text or more indication that the following results are from parter sites; for example where you have added the code:

  require("amazon.php");

...you could instead use:

  print "<p>Results from Amazon.com...</p>";
  require("amazon.php");

Hope this helps!

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by jmsobe on Fri, 2009-08-21 19:42.

Thanks a bunch, can other data be returned from amazon like product reviews or even just review star rating?

Submitted by jmsobe on Fri, 2009-08-21 19:52.

Another quick question I hope anyway. If I wanted to return a larger image in the feed can this be done easily? The medium image would be preferred. Thanks for your help

Submitted by daem0n on Mon, 2009-08-24 02:38.

Hi David,

Is there a way that if no results are found that I can return something like:

<p>Sorry, no results from Amazon were found! </p>

Thanks a lot! -Joe

Submitted by dmorison on Mon, 2009-08-24 08:33.

Hi Joe,

Sure - at the very end of the script simply add the following code:

  if (!$amazonItemCount)
  {
    print "<p>Sorry, no results from Amazon were found!</p>";
  }

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by daem0n on Mon, 2009-08-24 23:44.

Thanks as always!

-Joe

Submitted by Deanh01 on Fri, 2009-08-28 16:29.

Hi David
Is there a way to add that message to the ebay code?

Thank You
Dean

Submitted by dmorison on Fri, 2009-08-28 16:32.

Hi Dean,

It should be exactly the same, but using:

$ebayItemCount

...in place of

$amazonItemCount

...for example:

  if (!$ebayItemCount)
  {
    print "<p>Sorry, no results from eBay were found!</p>";
  }

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by Deanh01 on Fri, 2009-08-28 16:53.

Ok Works great
Thank You
Dean

Submitted by crounauer on Wed, 2009-09-09 17:40.

Hi David,

I went down the route of using OOP with the amazon API along with the neat little encrypton script you kindly wrote for us all.

$response = simplexml_load_file($awsUrl);

I am however getting this error message and I can't seem to figure out what is causing it. Could you perhaps point me in the right direction please?

An error occurred in script '/var/www/vhosts/xxxx.co.uk/httpdocs/electricals/modules/amazon/searchAM.php' on line 19: simplexml_load_file(http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService&amp;AssociateTag=xxxxx&amp;AWSAccessKeyId=xxxxx&amp;Operation=ItemSearch&amp;ResponseGroup=Medium,EditorialReview&amp;BrowseNode=560798&amp;MerchantId=All&amp;Condition=All&amp;Availability=Available&amp;SearchIndex=Electronics&amp;Keywords=electricalsHEWLETPACK+2015TONER+Q7553A&amp;Sort=salesrank&amp;Version=2009-01-06&amp;Timestamp=2009-09-08T22:38:56Z&amp;Signature=xxxxxxxxxxxxxx) [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: failed to open stream: HTTP request failed! HTTP/1.1 503 Service Temporarily Unavailable

Thanks,
Simon

Prices Compare

Submitted by dmorison on Wed, 2009-09-09 17:50.

Hi Simon,

That's indicating that an HTTP 503 error occurred during the request. The first thing to do is to debug the request by printing out the URL being constructed by your script and then requesting it manually in your web browser.

If it seems to be OK, double-check HTTP requests in general as it might be something caused by the local network, for example:

<?php
  $response 
file_get_contents("http://www.example.com/");
?>

If that generates the same error; it might be worth giving CURL a try as an alternative. The following block of code should be equivalent to the line of code in your post:

$ch = curl_init($awsUrl);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$xml = curl_exec($ch);
$response = simplexml_load_string($xml);

Hope this helps!

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by babrees on Thu, 2009-09-10 13:31.

Just putting this on my site and I'm getting products with 0.00 value. Is there anyway we can exclude anything with no price?

Also, I have managed to change the layout (through trial and error ) but I haven't been able to make the price bold - is there a way to do this?

I have the price in a column on it's own, so the line is:

print $config_currencyHTML.tapestry_decimalise($item["OFFERSUMMARY/LOWESTNEWPRICE/FORMATTEDPRICE"]);

---------
Jill

Submitted by dmorison on Thu, 2009-09-10 13:44.

Hi Jill,

Products with a zero price can be excluded by adding a check/return at the top of the myAmazonRecordHandler function in amazon.php. If you look for the following code:

  function myAmazonRecordHandler($item)
  {

...and then INSERT on the next line:

  if (tapestry_decimalise($item["OFFERSUMMARY/LOWESTNEWPRICE/FORMATTEDPRICE"])==0.00) return;

The best way to bold the price is probably a style attribute on the table cell. To do this, look for the following code around line 32:

    print "<td valign='middle' align='center'>";

...and REPLACE this with:

    print "<td valign='middle' align='center' style='font-weight:bold;'>";

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by KGP on Tue, 2009-10-13 16:24.

what is the delay when fetching results from both amazon and local db (price tapestry db i mean)?

cause i was having problems all the time i had amazon api on cause the request and results would delay the whole search results page and/or product page (in case i did both or either of search results or product)

Submitted by dmorison on Tue, 2009-10-13 16:29.

Hi,

There will be a slight delay caused by the round-trip to the API server; however what you could do, if Amazon products are being displayed at the end of the page; would be to issue a flush() statement immediately before the call to require("amazon.php"); for example:

  flush();
  require("amazon.php");

Bear in mind that if you are using a table design the browser may actually hold back displaying cell contents until the closing table tag has been received...

Hope this helps!

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by Rocket32 on Sat, 2009-10-17 17:17.

I am having products displayed with prices $0.00. When I click on the product, it has a price on the Amazon Site.
Is there a reason that the prices are not showing correctly for some products?

Submitted by dmorison on Mon, 2009-10-19 09:40.

Hi,

That's interesting that a different price is displayed on the destination that is returned in the API. See this comment above regarding suppressing zero prices as shown in the API; but if you could let me know what search index you are using (if not "Blended"), and the keywords being used in the query I'll run them through my test installation and look at the XML response to see if there is something we should be doing slightly differently (such as looking at a different price field in this instance...)

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by moggiex on Sat, 2009-11-07 15:18.

For anyone else having problems working out why this isn't working for them see the post below from http://www.pricetapestry.com/node/2972

Hi Keeop,
Did you upgrade to the authenticated version from this thread?
As a simple debug exercise to see what is being returned from Amazon (which will normally indicate the problem), look for the following code on line 131:
$url = amazonSign($url,$amazonSecretAccessKey);
...and then ADD the following code on the next line:
$xml = file_get_contents($url);
print $xml;exit();
Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

However, I'm still receiving the error, after trying a few different methods, I've also verified that these are set in php.ini:

allow_url_include = On
allow_url_fopen = On

And that file_get_contents('http://www.google.co.uk');

Yet I still get:

Warning: file_get_contents(http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService&Version=2009-03-01&Operation=ItemSearch&AWSAccessKeyId=***&AssociateTag=***-21&ResponseGroup=Medium&SearchIndex=Electronics&Keywords=card&Timestamp=2009-11-07T14:09:19Z&Signature=*) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/sites/***.co.uk/html/amazon.php on line 140

Any ideas?

Matt

Submitted by moggiex on Sat, 2009-11-07 15:35.

Bingo, I found the issue!

The clocks moved a few weeks back, my server was still running an hour behind, I updated the time on it and it works fine!

Set Date/Time from the command line/SSH:
http://www.cyberciti.biz/faq/howto-set-date-time-from-linux-command-prompt/

Matt

Submitted by dmorison on Sat, 2009-11-07 19:23.

Subtle!

Thanks for pointing that out, Matt!

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by philstone on Tue, 2009-11-17 14:40.

Hi David

hope your well

was wondering if you could help me, products appearing in the amazon part of the script that cost over £1000 appear wrong

eg: Amazon Price of a product - £1,493.84 - but £1.49 shows

would appreciate your knowledge in fixing this
thanks

Phil Stone
www.buy24-7.net

Submitted by dmorison on Tue, 2009-11-17 15:02.

Hi Phil,

This is happening because Price Tapestry is by default set-up to handle the comma as a decimal point; as it common in some product feeds (particularly in the currencies of countries where a comma is used like a decimal point).

To fix this, look for the following code on line 33:

    print $config_currencyHTML.tapestry_decimalise($item["OFFERSUMMARY/LOWESTNEWPRICE/FORMATTEDPRICE"])."<br />";

...and REPLACE this with:

    $item["OFFERSUMMARY/LOWESTNEWPRICE/FORMATTEDPRICE"] =
      str_replace(",","",$item["OFFERSUMMARY/LOWESTNEWPRICE/FORMATTEDPRICE"]);
    print $config_currencyHTML.tapestry_decimalise($item["OFFERSUMMARY/LOWESTNEWPRICE/FORMATTEDPRICE"])."<br />";

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by philstone on Tue, 2009-11-17 15:16.

thanks so much dave!!

Best support I've ever had with any product!!!

thanks

Phil Stone
www.buy24-7.net

Submitted by jim on Mon, 2009-11-23 10:26.

Hi David,

Got this mod working nicely... but is it possible if there isn't an image, for a spacer.gif to be used instead?

Submitted by dmorison on Mon, 2009-11-23 10:36.

Hi Jim,

Sure - on line 24 of amazon.php you will find:

print "<img width='80' src='".$item["SMALLIMAGE/URL"]."' />";

...REPLACE this with:

if (!$item["SMALLIMAGE/URL"]) $item["SMALLIMAGE/URL"] = "/images/spacer.gif";
print "<img width='80' src='".$item["SMALLIMAGE/URL"]."' />";

(i've used /images/ infront of spacer.gif; but if it's in the top level folder just use /spacer.gif)

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by jim on Tue, 2009-11-24 00:01.

perfect. thanks David!

Submitted by jim on Mon, 2009-11-30 04:43.

Hi David, I'm encountering a small problem whilst trying getting my page to validate (XHTML 1.0 Transitional) using the Amazon API mod.

The site was tripping up when the title of an item contained an apostrophe...
To try and fix that I tried to use some hacky code below (eg title=\"".) but now I also have problems when the item title contains a quote (eg. Kiss With a Fist [7" VINYL])

    print "<tr>";
    print "<td width='40'>";
if (!$item["SMALLIMAGE/URL"]) $item["SMALLIMAGE/URL"] = "/pt/images/spacer-40x40.gif";
    print "<a title=\"".$item["ITEMATTRIBUTES/TITLE"]."\" target='_blank' href='".$item["DETAILPAGEURL"]."'><img width='40' height='40' src='".$item["SMALLIMAGE/URL"]."' alt=\"".$item["ITEMATTRIBUTES/TITLE"]."\" /></a>";
    print "</td>";
    print "<td width='30' valign='middle' align='center'>";
    print "<b><a title=\"".$item["ITEMATTRIBUTES/TITLE"]."\" class='bandlink' target='_blank' href='".$item["DETAILPAGEURL"]."'>".$config_currencyHTML.tapestry_decimalise($item["OFFERSUMMARY/LOWESTNEWPRICE/FORMATTEDPRICE"])."</a></b>";
    print "</td>";
    print "<td width='224' valign='middle'>";
    print "<b><a title=\"".$item["ITEMATTRIBUTES/TITLE"]."\" target='_blank' href='".$item["DETAILPAGEURL"]."'>".$item["ITEMATTRIBUTES/TITLE"]."</a></b>";
    print "</td>";
    print "</tr>";

Is there a way to make the title 'safe' ?
(I assume the Amazon API doesn't render ampersands as & either - is it easy to replace space&space with space&space ?)

thanks :)

Submitted by dmorison on Mon, 2009-11-30 09:43.

Hi Jim,

Where adding "unknown" data into an XHTML attribute; use htmlentities(), for example:

title='".htmlentities($item["ITEMATTRIBUTES/TITLE"],ENT_QUOTES)."'

The ENT_QUOTES constant makes sure that both ' and " are encoded.

Hope this helps!

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by jim on Mon, 2009-11-30 11:07.

Thanks David :)

Submitted by philstone on Wed, 2009-12-09 11:25.

Hi David

trying to tweak the amazon.php script, on some of my site i have specialized products into sub categories eg video games, the problem is on the ds pages, amazon results show xbox and playstation games also, is there anyway i could add text after the keywords eg ds, so that when it searches amazons database it returns the results keywords DS?

regards

Phil Stone
www.buy24-7.net

Submitted by dmorison on Wed, 2009-12-09 15:11.

Hello Phil,

Sure - simply replace the code to require amazon.php with the following, edited as required for each installation:

  $q .= " DS";
  require("amazon.php");

Note the space in front of the keywords to append to $q!

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by urmyworld on Sat, 2009-12-26 22:37.

Hello David,

I have installed this script on my website but I am not getting any results whatsoever from Amazon.co.uk

The website is television related and I would imagine there is plenty of related content on Amazon.

I have checked my website search with the same search in Amazon and products do show in Amazon.

My access keys are correct

any ideas?

best regards

David S S

Submitted by dmorison on Sun, 2009-12-27 15:41.

Hi David,

If you're sure everything is correct, the thing to do is to print the response from Amazon to the screen, which will normaly indicate the reason for there being no results. To do this, in place of the following code starting at line 131:

    $url = amazonSign($url,$amazonSecretAccessKey);
    // fetch the response and parse the results
    MagicParser_parse($url,"myAmazonRecordHandler","xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/");

Use:

    $url = amazonSign($url,$amazonSecretAccessKey);
    $xml = file_get_contents($url);
    // fetch the response and parse the results
    print "<textarea cols='80' rows='4'>".htmlentities($xml)."</textarea>";
    MagicParser_parse("string://".$xml,"myAmazonRecordHandler","xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/");

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by urmyworld on Thu, 2010-01-07 10:55.

Hello David,

I did as you suggested above and the error messages onscreen are as follows:

Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /home/XXXXXXX/public_html/amazon.php on line 132

Warning: file_get_contents(http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService&Version=2009-03-01&Operation=ItemSearch&AWSAccessKeyId=XXXXXXXXXXXXXX&AssociateTag=XXXXXXXXX-21&ResponseGroup=Medium&SearchIndex=Electronics&Keywords=Sony+KDL+52V4000&Timestamp=2010-01-07T10:48:49Z&Signature=18CC%2FPRK5QPI%2FzNh33FukoJyUEJLQPm7JKPwSJEdB6k%3D) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /home/XXXXXXX/public_html/amazon.php on line 132

any ideas?

best regards

David S S

Submitted by dmorison on Thu, 2010-01-07 11:00.

Hi David,

That indicates the fopen()'ing URLs is disabled on your PHP configuration. Now, before contacting your host to see if they would be happy to enable this for you account (I assume that you are running on a shared hosting account rather than VPS / Dedicated server) it's worth trying CURL as an alternative method for retrieving the remote URL. To do this, in place of the original code:

    $url = amazonSign($url,$amazonSecretAccessKey);
    // fetch the response and parse the results
    MagicParser_parse($url,"myAmazonRecordHandler","xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/");

Have a go with:

    $url = amazonSign($url,$amazonSecretAccessKey);
    // fetch the response and parse the results
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $xml = curl_exec($ch);
    MagicParser_parse("string://".$xml,"myAmazonRecordHandler","xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/");

Hope this helps!

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by urmyworld on Thu, 2010-01-07 11:12.

Hello David,

I tried that and now I am getting

Parse error: syntax error, unexpected T_STRING in /home/xxxxxxx/public_html/amazon.php on line 137

best regards

David S S

Submitted by dmorison on Thu, 2010-01-07 11:17.

Hi David,

Sorry - there was a semi-colon missing from...

    $xml = curl_exec($ch);

I've corrected the code above also.

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com

Submitted by urmyworld on Thu, 2010-01-07 12:23.

Hello David,

Happy New Year!!

All fixed, thank you for your help once again.

Although I have just noticed that the Amazon links do not open in a new window.

Can this be changed?

best regards

David S S

Submitted by dmorison on Thu, 2010-01-07 12:38.

Hi David,

Sure - the link is generated by this code (line 34):

    print "<nobr><a href='".$item["DETAILPAGEURL"]."'>More Information</a></nobr>";

...simply replace with:

    print "<nobr><a target='_BLANK' href='".$item["DETAILPAGEURL"]."'>More Information</a></nobr>";

Cheers,
David.
--
Developer, Price Tapestry
General PHP, MySQL and Affiliate Marketing tech help for Price Tapestry customers at davidmorison.com