Tech-Evangelist

Technical Articles, Musings and Opinions from Tech-Evangelist

  • Home
  • About
  • Guidelines
Previous article: HQFS 3D Movies – How They Work
Next article: A Useful Error 404 Page for WordPress

Amazon Top Sellers PHP Code

August 20, 2010 By Craig - Copyright - All Rights Reserved

Amazon.com has one of the largest affiliate marketing programs on the web. Their wide selection of products makes them an attractive merchant to work with. While there are several scripts available that interact with Amazon’s XML database and provide real-time displays of products, pricing descriptions, user comments and images, it is hard to find a script that displays Amazon’s Top Ten Sellers.

One of the unique aspects of working with Amazon’s affiliate program, which they refer to as Amazon Associates, is that no database is required when accessing and displaying product or category information. This means that an Amazon affiliate marketing web site can be fully automated and require little or no maintenance. The only maintenance required is when Amazon periodically updates the requirements for accessing their data, or when they change or delete browse nodes. Data can be accessed using a variety of programing languages using REST or SOAP. REST is faster and much easier to work with and is used by about 85% or affiliates, so our sample script uses REST.

A browse node is Amazon jargon for a product category. Browse nodes can be looked up at BrowseNodes.com or sometimes extracted from URLs on the Amazon site. However, extracting browse nodes from Amazon URLs can be problematic, because URLs for newer product tend to use a new format that does not contain a working browse node that associates can use.

What will the script do?

Amazon publishes a list of Top Ten Sellers on their web site for most product categories. The list is updated hourly and can be used to draw users in for the most popular products currently sold on Amazon. This PHP script will display that list for MOST categories. I say most because not every category has a top ten sellers list, but the most popular product categories do.

What do I need to use this script?

Some PHP skills would help, but you only need minimal PHP sills to understand this script. The script utilizes SimpleXML to pull in the Top Sellers data, so you will need a server that runs PHP 5 or higher. PHP 4 servers do not run SimpleXML. You also need a server that can make external calls (requests from another web site). Almost all hosting companies allow this, but a few do not. If you try to run the script on a server that does not have Internet access, the script will not work. If you try to run it on your PC as localhost and you have a personal firewall, the firewall may prevent the call to the Amazon server. Keep these things in mind if you are testing the script.

You also need to have an Amazon Associates account and an Amazon Web Services account. The Amazon Associates account is where you can set up tracking IDs, which are used to identify you so that you receive the appropriate commissions. The Amazon Web Services account is where you will be assigned an AWS Access Key and an AWS Secret Key. The AWS Access Key is a username that you will need to access the Amazon XML database. The AWS Secret Key is a type of password that will also be used to securely encode portions of the request for Amazon data. Both are requires since July of 2009, when Amazon began requiring secure signatures in order to access their data.

<?php
$AWSAccessKeyId = "";
$AWSSecretKey = "";
$TrackingID = "";
$browseNodeID = "172659"; // HDTVs

// no changes needed below this line 

$Timestamp = gmdate("Y-m-d\TH:i:s\Z");
$Timestamp = str_replace(":", "%3A", $Timestamp);
$item = "";
$rank = 0;

$String ="AWSAccessKeyId=$AWSAccessKeyId&".
  "BrowseNodeId=$browseNodeID&".
  "Operation=BrowseNodeLookup&".
  "ResponseGroup=TopSellers&".
  "Service=AWSECommerceService&".
  "Timestamp=$Timestamp&".
  "Version=2009-01-06";
  
$Prepend = "GET\nwebservices.amazon.com\n/onca/xml\n";
$PrependString = $Prepend . $String;
  
$Signature = base64_encode(hash_hmac("sha256", $PrependString, $AWSSecretKey, True));
$Signature = str_replace("+", "%2B", $Signature);
$Signature = str_replace("=", "%3D", $Signature);

$BaseUrl = "http://webservices.amazon.com/onca/xml?";
$SignedRequest = $BaseUrl . $String . "&Signature=" . $Signature;

if(!$xml = simplexml_load_file($SignedRequest))
{
   echo "Top Sellers not available.<br>";
}
else 
{
  foreach ($xml->BrowseNodes->BrowseNode->TopSellers->TopSeller as $item)
  {
     $rank++;
     echo $rank.". <a href=\"http://www.amazon.com/exec/obidos/ASIN/".$item->ASIN;
     echo "/ref=nosim/".$TrackingID."\" rel=\"nofollow\">".$item->Title;
     echo "</a><br />";
  }
}
?>

Let’s walk through this.

The $AWSAccessKeyID and $AWSSecretKey are the items you will need to access Amazon’s XML database. The $TrackingID is your Amazon Associated account ID, or if you set up separate IDs for each web site, it is the ID assigned to the web site where you wish to install the script.

The $BrowseNodeID is the category ID for any valid Amazon browse node that displays Top Sellers. You may have to test several IDs, because as I said, not all browse nodes have Top Ten Sellers. The browse node used in the example is for HDTVs.

The script sets up the variables and values that it needs on lines 9 through 30, including the encoded request URL that will be used to pull in the data via REST.

The XML data is requested on line 32. If the Amazon server is busy, “Top Sellers not available” will be displayed. This occasionally happens with any request from Amazon, so the message is a friendly way of handling an error. If the request is successful, the top ten sellers are extracted in an XML file format. The foreach loop on line 38 begins to parse the data from the XML file.

Lines 41 through 43 display the data in a list of stack-ranked links that include your Amazon tracking ID.

This is what the output from this script will look like. You can doctor it up with a little CSS and display it on almost any web site that runs PHP.

1. Toshiba 15LV505 15.6-Inch Widescreen LCD TV with Built-in DVD Player (Black)
2. Samsung LN46C630 46-Inch 1080p 120 Hz LCD HDTV (Black)
3. Samsung PN42C450 42-Inch 720p Plasma HDTV (Black)
4. LG 42LD550 42-Inch 1080p 120 Hz LCD HDTV
5. Samsung LN40C630 40-Inch 1080p 120 Hz LCD HDTV (Black)
6. Panasonic VIERA TC-P50G25 50-Inch 1080p Plasma HDTV
7. Samsung UN46C6300 46-Inch 1080p 120 Hz LED HDTV (Black)
8. Panasonic TC-L32X2 32-Inch 720p LCD HDTV with iPod Dock
9. LG 26LE5300 26-Inch 720p 60Hz LED LCD HDTV
10. Samsung LN55C650 55-Inch 1080p 120 Hz LCD HDTV (Black)

The links are set up with the rel=”nofollow” attribute to help avoid problems with search engines that do not like affiliate links. It also avoids problems when Amazon decides to redirect a URL. The links are set up with Amazon’s ref=nosim parameter, which tells Amazon to display the specific product page, rather than defaulting to a similar products page as they sometimes tend to do.

We hope you find this script to be useful. We will be adding additional affiliate scripts for Amazon and other affiliate networks.

Filed Under: Affiliate Marketing, Internet Marketing, Web Site Development

Comments

  1. Emma says

    October 14, 2010 at 4:51 pm

    Useful tutorial – I am looking at going beyond displaying just one or two Amazon items, so pulling them from a Top Seller list like this is a great idea for me. Can this script be edited to display product images as well, or excerpts from the description?
    Cheers,
    Emma

  2. Craig says

    October 24, 2010 at 3:55 pm

    Hi Emma

    The Top Sellers response group always returns the top 10 items.

    You cannot retrieve the product images or descriptions with the Top Sellers response group call to Amazon’s servers. The only data returned is the list of ASINs and product titles. However, you can use the ASINs that you’ve collected to request the image and description by saving the ASINs in an array and making separate calls to Amazon’s servers. That, however, complicates this simple script considerably.

  3. werapun seemee says

    December 31, 2010 at 4:21 pm

    i’m test code on localhost
    show error !
    Help me please .

    Warning: simplexml_load_file( { link deleted } [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in C:\AppServ\www\amazon\index.php on line 32

    Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity { link deleted } in C:\AppServ\www\amazon\index.php on line 32
    Top Sellers not available

  4. Craig says

    December 31, 2010 at 6:54 pm

    Try using a browse node ID. The ID in the link that you provided is B0043B7WDK. That is an ASIN, which is a product ID. You need to use a browse node ID. A browse node ID represents a product category, not an individual product.

    A browse node ID is all numeric. Try using the browse node ID used in the sample code and it should work.

    You can look up browse node IDs at the following site:

    BrowseNodes.com

  5. Dave says

    February 10, 2011 at 4:23 pm

    Hey Craig,

    thanks for the excellent tutorial, definitely helped me on my way. i have a question though, how is it possible to send two different browsenode ids in the request.

    thanks,

    -dave

  6. Craig says

    February 10, 2011 at 4:42 pm

    Hi Dave

    You cannot submit two browse nodes at one time, but you can set up a loop that reads the results from each browse node into an array and then you can do anything that you want with the array. That works well if you want to focus on multiple browse nodes within a major product category, such as sporting goods.

  7. Dave says

    February 10, 2011 at 7:03 pm

    Hi Craig ,

    thanks for the fast response, thats exactly what im trying to do, my only question is, where in the code would i put the loop?

    thanks in advance,

    -dave

  8. Craig says

    February 10, 2011 at 9:55 pm

    I’d have to rewrite the code to explain it is detail. That takes more time than I have right now.

    Place each browse node ID in a variable or an array.

    Start a for loop at line 13. The loop would end at line 46. Set it up to loop twice.

    On the first pass, read the first browse node ID and pull in the top sellers.

    On the second pass, read the second browse node ID and pull in those top sellers.

    If you just made those changes to the code, the results would display the Top Sellers and links for the first browse node, followed by the Top Sellers and links for the second browse node.

    If you modify the foreach loop to dump the results into an array, you can sort them or do whatever you want with them.

  9. Tommy says

    April 20, 2011 at 4:31 am

    Thanks Craig, this very helping me. But now i confuse how to put amazon and adsense code like your page above. It’s because my website is in htm and not php. Can you give me suggestion?

    Thanks before.

  10. Craig says

    April 20, 2011 at 11:34 am

    Hi Tommy

    You can use AdSense on any type of web page. PHP code will only run on a site that parses PHP. If your site is on a Linux server, you can set up the .htaccess file so that .htm pages are run through the PHP parser. You can then run PHP code on an .htm page.

    Check out this article: How to Convert HTML Pages to Run PHP

  11. tyleraz says

    April 22, 2011 at 3:28 pm

    Hi Craig. how is it possible to show more than 10 items and divide them into more pages.
    Thanks

  12. Craig says

    April 22, 2011 at 5:12 pm

    Hi tyleraz

    Amazon only returns 10 items at a time on average. You cannot force the XML feed to return more.

    Browse node categories can be divided into pages by requesting the first 10 items, then the second 10 items, and so on. Each group of 10 becomes a separate category page. This requires codes that is more complex than in this example. Perhaps I will add that as a future tutorial.

  13. Kevin says

    July 12, 2011 at 7:56 am

    Thank you this works great. Question though:

    How do I get the images of the products from this?

    I see that the array is only containing:

    SimpleXMLElement Object ( [ASIN] => B0006GYLOE [Title] => Harry Potter Gryffindor House Scarf (Purple & Gold) )

  14. Craig Mazur says

    July 18, 2011 at 8:14 am

    Hi Kevin

    You cannot get the image URLs with the Top Sellers request. To get the image URLs you have to save the Top Sellers ASINs to an array or a database and then do an ItemLookup request for each ASIN to get the image URLs.

    If you need to do this, you should be saving the data to a MySQL table. A real-time request for the Top Sellers and image URLs for each of the ten would likely take too much time and would waste your allocation for requests. We do a large update for multiple Top Sellers browse nodes every day at midnight and use the resulting MySQL table to display the results throughout the day. It is much more efficient to do it that way.

  15. max says

    October 8, 2011 at 11:11 am

    Thanks for this great tutorial.
    I have a question , this sample of code gives us the list of 10 Amazon Best Sellers, but i wonder if this code could gives us the list of other criteria such as Top Rated, Hot New Releases and Most Gifted products as this criteria have the same NODE, maybe by add change and this lines…

    foreach ($xml->BrowseNodes->BrowseNode->TopSellers->TopSeller as $item)
    foreach ($xml->BrowseNodes->BrowseNode->TopRated->TopRated as $item)

    I dunno how to implement this, coz i didn’t have any PHP skill, I just wonder if its could works, could you give us some of the final code.? thanks craig

    best regard,
    max

  16. Craig says

    October 9, 2011 at 7:52 am

    Hi Max

    Unfortunately, that won’t work. There isn’t much info returned in the Amazon Top Sellers XML request. The response only returns the ASIN and Title.

  17. al says

    November 26, 2011 at 9:29 pm

    Good, separate variables into config.php or something and include it for more node id. I’ll use it for my specific project. Thanks for sharing.

Categories

  • Affiliate Marketing
  • CSS Tutorials
  • FileZilla Tutorials
  • Home Theater
  • Internet Marketing
  • Internet Technology
  • Kindle Tips
  • MySQL Tutorials
  • Online Auction Tips
  • Paint Shop Pro Tutorials
  • PHP Tutorials
  • Tech News
  • Thunderbird Tutorials
  • Video Production
  • Web Site Development
  • WordPress Tutorials
Content and images are copyrighted by Tech-Evangelist.com and others

Copyright © 2023 Tech-Evangelist.com - All Rights Reserved
Posted code samples are free to use. Do not reproduce or republish articles or content on another web site.

Privacy Policy : Terms of Use