Add Amazon's AWS Content to HTML Pages

Discussion in 'Amazon' started by myhart, Dec 10, 2006.

  1. #1
    I am starting this topic because of recent comments concerning the difficulty of working with Amazon's AWS. I am certainly not a programmer and only have a very basic understanding of PHP and HTML. Even so I have been able to piece together a few simple Amazon affiliate scripts. As such I thought I would share some of the knowledge I have gained over the last couple years.

    As a preview of what is possible with a few lines of PHP code see the following page.

    Antique Furniture Repair and Refinishing

    The content in the blue table is displayed using Amazon's AWS. Please note the page url and be aware that I am not using an iframe or mod_rewrite.

    I plan to divide this concept into several posts and will post more as time allows.
     
    myhart, Dec 10, 2006 IP
  2. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Before parsing the XML delivered by Amazon. We need to decide what Amazon information we need. As well as confirm that it is being delivered to our XML parser.

    The following code is pretty simple but it does require adding your Subscription Id and Associates ID to the Amazon query url. To make it easier we can use variables. The asin can be changed to match any product you desire.

    The ResponseGroup determines what and how much XML info is delivered to the XML parser. I have selected "Small" which doesn't return much info. However it should convey the idea without becoming to overwhelming at first glance. Amazon allows for selecting from among quite a few different response groups for making our requests. For now you might also try "Medium" and "Large" to compare the results displayed. Also we can combine different response groups to acheive the desired results such as "Small,Images".

    The Version at the end of the Amazon query url is optional. However as I want to keep things as simple as possible. I am using an older Version date which limits the possible image urls to small, medium and large.

    To view Amazon's XML simply place the code below in a PHP file. Then click on the XML link. In a future post I will cover enabling HTML files to function as PHP using .htaccess. As well as post a very simple parser that uses PHP. Which can allow us to easily add all kinds of Amazon affiliate content to our HTML pages.

    <?php
    $SubscriptionId = "";
    $AssociateTag = ""; // Associates ID
    $asin = "1580110061";
    $ResponseGroup = "Small";
    
    $url = 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AssociateTag='.$AssociateTag.'&SubscriptionId='.$SubscriptionId.'&Operation=ItemLookup&ItemId='.$asin.'&ResponseGroup='.$ResponseGroup.'&Version=2005-02-23';
    
    echo '<a href='.$url.'>XML</a>';
    ?>
    PHP:
     
    myhart, Dec 10, 2006 IP
  3. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you have lots of original content on "old" HTML pages? You probably don't want to change the urls simply to allow the use of PHP? Mod_rewrite would be one possible solution assuming that your host has it enabled. However figuring out mod_rewrite can sometimes be a challenge in itself.

    For Apache servers adding the following to your .htacess file may be an easier/better solution? It works fine with my VPS though I assume that this can vary from host to host!

    RemoveHandler .html .htm
    AddType application/x-httpd-php .php .htm .html
    Code (markup):
    I did quite a bit of research before using the above code. Without finding any major problems mentioned because of it's use. However please do your own research if in doubt! The one negative that I found mentioned was that an HTML page would load slightly slower (at the speed of a normal PHP page).

    The above code can also be used to enable HTML files to function as PHP in individual directories. Simply add a .htacess file to the desired directory and insert the code.

    In my next post I will link to the source code of a simple/free parser that I found while doing research on parsing Amazon's XML feed. By making a few minor changes to this code it will funtion fine with Amazon's ECS4 for simple affiliate projects.
     
    myhart, Dec 11, 2006 IP
  4. mr_dean

    mr_dean Peon

    Messages:
    280
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    myhart, is it easy using AWS to get product link which fits best to your description? I could extract descriptions from database. Although, only 1/5 of products with those descriptions exists at Amazon :(.
     
    mr_dean, Dec 11, 2006 IP
  5. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    What I hope to show first is a simple way of getting AWS info using a specific "asin".

    However this same basic code should allow us to "search" for products using keywords. To narrow our search even farther (get better results) we can search individual browse nodes using keywords. Much like at the store I am working on.

    http://www.antiquefurniture.us/store/

    When doing an Amazon keyword search we need to limit the number of keywords for best results. Guess a lot will depend on what your "description" consists of? A model number should work quite well if Amazon handles that product or accessories.

    Hope this helps for now? Once we have the basics in place AWS will enable us to access lots of product info! :rolleyes:
     
    myhart, Dec 11, 2006 IP
  6. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The idea of parsing XML with PHP's built-in PHP XML parser is certainly not new. Paul Bausch in his book Amazon Hacks gives us an example of doing it using Amazon's AWS3 XML feed.

    http://www.oreilly.com/pub/h/462#code

    With the introduction of Amazon's ECS4 which delivers more XML info than AWS3. Parsing Amazon's feed has become somewhat more complicated. However parsing with PHP's built-in parser still works and the code can be quite simple for basic projects.

    The following code updates the Amazon query url to work with ECS4. A similar parser is used as part of the aws4class.php file in ASM2.

    Please note that more case statements can be added to extract additional info from the array. To check if the desired XML info is available simply uncomment the following line.

    //echo '<a href='.$url.'>xml</a><br><br>';
    Code (markup):
    If we need more XML info we can modify the ResponseGroup.

    <?php
    $SubscriptionId = "";
    $AssociateTag = ""; // Associates ID
    $asin = "0875307345";
    $ResponseGroup = "Small,Images";
    
    $url = 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AssociateTag='.$AssociateTag.'&SubscriptionId='.$SubscriptionId.'&Operation=ItemLookup&ItemId='.$asin.'&ResponseGroup='.$ResponseGroup.'&Version=2005-02-23';
    
    //echo '<a href='.$url.'>XML</a><br><br>';
    
    $xml = @implode("",file($url));
    $parser = xml_parser_create();
    xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
    xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
    xml_parse_into_struct($parser,$xml,$values,$index);
    xml_parser_free($parser);
    
    foreach ($index as $key=>$val) {
      switch ($key) {
      
    case "URL":
      $URL = $values[$val[0]]['value'];       
      break;
    case "Title":     
      $Title = $values[$val[0]]['value'];   
      break;
    case "DetailPageURL":       
      $DetailPageURL = $values[$val[0]]['value'];       
      break;
    }
    }
    ?>
    
    <a href="<?=$DetailPageURL ?>"><img border="0" src="<?=$URL ?>" alt="<?=$Title ?>">
    Code (markup):
    An example of the above code in use can be seen on the following page. Clicking on the small image takes us to the appropriate Amazon product page with the Affiliate ID embedded.

    http://www.antiquefurniture.us/atlanta_antiques.html

    There are lots of possibilities that we can explore with this simple code! In my next post I will show how to select from small, medium and large images.
     
    myhart, Dec 12, 2006 IP
  7. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The script shown above displays a small size image. However we can easily change it to display other image sizes.

    Using the case statements allows us to display the first item matching our request found in the array. However this can easily be changed by replacing the "0" in the following line with small image = "0", medium = "1" and large = "2".

    $URL = $values[$val[0]]['value'];
    Code (markup):
    In my next post I will post a way of changing AWS content on page load.
     
    myhart, Dec 12, 2006 IP
  8. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Once we have the basic script working its just a matter of deciding what Amazon AWS info we want displayed and how to display it. An image alone probably won't get us many clicks? As such we need to select additional AWS content that is relevant to our webpage. A couple additional case statements will allow us to display the the book's author and ASIN.

    However in this example I wanted to add something more exciting to attract attention to the Amazon affiliate product displayed. As such I decided to add customer reviews. While that in itself is certainly nothing new as many Amazon scripts already feature them! However the way I have chosen to display the customer reviews may be unique?

    First we need to add "Reviews" to the ResponseGroup. So that Amazon knows to return the customer review info as part of the XML feed.

    $ResponseGroup = "Small,Images,Reviews";
    Code (markup):
    Then we add additional case statement allowing us to display the chosen info. I decided to display the number of available reviews as well as the date of each review. All pretty basic content used on many existing Amazon affiliate websites.

    Now to the fun part! From working with the image case statement. We know that our script only returns one seleted result from the array at a time. While this can be considered a shortcoming? We can easily turn it into an asset for our use!

    As with the image case statement we can select which review to display with the following line of code. Amazon's XML returns up to five customer reviews for the selected ASIN at a time. So we simply replace "0" with 0 thru 4 to select the desired review.

    $Content = $values[$val[0]]['value'];
    Code (markup):
    The Amazon product info displayed on our webpage is now somewhat unique. Containing only 1 review rather than the 3 or 5 shown on most webpages. Mixed in with our original content hopefully this will allow us to avoid the dreaded Supplemental Results?

    However we can take this concept one step farther. So that the review shown randomly updates on each page reload. PHP makes this feature quite easy to add using the following code!

    srand ((double) microtime( )*1000000);
    $r = rand(0,4);
    Code (markup):
    The above code creates a variable $r with a value from 0 thru 4. Which is then added to the case statement for selecting our customer review on a random basis. So that Date and Summary match the Content we also need to update their case statements as shown below.


    case "Date":
              $Date = $values[$val[$r]]['value'];
              break;
    
       case "Summary":
              $Summary = $values[$val[$r]]['value'];
              break;
    
        case "Content":
              $Content = $values[$val[$r]]['value'];
              break;
    
    Code (markup):
    To see the results of adding random Amazon AWS content to a webpage refresh the following page a couple times. The blue table at the top of the page contains the Amazon customer review. Please be aware that not all comments contained in the reviews are positive! :eek:

    http://www.antiquefurniture.us/antique-auction-schedule.html

    In my next post I will address Amazon products that display no image results. As well as post the above script using a table to display the results.
     
    myhart, Dec 13, 2006 IP