Amazon Hacks Free Open Book

Amazon Hacks

Previous Section Next Section

Hack 78 Program AWS with PHP

figs/expert.giffigs/hack78.gif

Parsing XML with the built-in PHP XML parser can be tricky. But once you see how it's done, you'll be parsing Amazon XML in no time.

Using someone else's wrapper functions to handle Amazon Web Services requests is a nice shortcut to building applications. But the time inevitably comes when you want to do something that existing wrappers don't handle. You can either look under the hood and tinker with or add to someone else's work, or you can build your own scripts from scratch.

The following bit of PHP might be a good start if you're building something from the ground up. It uses PHP's built-in XML parser to handle AWS responses, so there's nothing new to install. PHP's parser is James Clark's expat, and you can find the documentation at http://www.php.net/xml.

Given an ASIN, this example requests the lite XML response and formats the results in an HTML table.

78.1 The Code

This code builds an AsinSearch Amazon request URL with the asin value passed in through the querystring, and retrieves the XML with the fopen( ) function.

PHP's xml_parse_into_struct function turns the AWS response into two arrays—one with tag names ($index) and the other with values ($values). Looping through the array, the script then sets the value when it finds the XML tag it's looking for. With the values set, it prints the HTML to the page with the values in place. Create a file called ASINsearch.php containing the following code:

<?php
// Set Local variables
$dev_token = "insert developer token";
$aff_tag = "insert affiliate tag";
$asin = $_GET['asin']; //grab ASIN from Querystring
$xml = "";
$key = "";
$values = "";

// Set Amazon variables
$ProductName = "";
$ImageURL = "";
$OurPrice = "";
$AmazonURL = "";

// Build Amazon XML Query URL
$URL = "http://xml.amazon.com/onca/xml3?t=" . $aff_tag . 
       "&dev-t=" . $dev_token . 
       "&type=lite" . 
       "&f=xml" . 
       "&AsinSearch=" . $asin;

// Uncomment this line to see the URL
//print $URL;

// Get Amazon XML Query Results
$aurl=fopen($URL,"r");
while (!feof ($aurl))
    $xml .= fgets($aurl, 4096);
fclose ($aurl);

// Fire up the built-in XML parser
$parser = xml_parser_create(  ); 
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

// Set tag names and values
xml_parse_into_struct($parser,$xml,$values,$index); 

// Close down XML parser
xml_parser_free($parser);

// Loop through the XML, setting values
foreach ($index as $key=>$val) {
    switch ($key) {
      case "ProductName":
          $ProductName = $values[$val[0]]['value'];
          break;
      case "ImageUrlMedium":
          $ImageURL = $values[$val[0]]['value'];
          break;
      case "OurPrice":
          $OurPrice = $values[$val[0]]['value'];
           break;
    }
}
?>
<table width="200" border="1" cellpadding="5" bgcolor="#cccccc">
<tr><td align="center">
<a href="http://www.amazon.com/o/ASIN/<?= $asin ?>/ref=nosim/<?= $aff_tag [RETURN]
?>">
    <img src="<?= $ImageURL ?>" border="0">
</a>
<br>
<?= $ProductName ?>
<br>
<b><?= $OurPrice ?></b>
</td></tr>
</table>

78.2 Running the Hack

Upload ASINsearch.php to your server and call it with an ASIN in the querystring:

http://your.server/ASINsearch.php?asin=insert ASIN

If all goes according to plan, you should see a nicely formatted table with the image of the product, its name, and the current Amazon price. The image should link to Amazon using your associate tag.

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Credits
    Foreword
    Preface
    Chapter 1. Browsing and Searching
    Chapter 2. Controlling Your Information
    Chapter 3. Participating in the Amazon Community
    Chapter 4. Selling Through Amazon
    Chapter 5. Associates Program
    Chapter 6. Amazon Web Services
    6.1 Hacks #76-100
    6.2 What Are Web Services?
    6.3 Why Expose an API?
    6.4 What You Need
    6.5 What You Can Do
    6.6 Making Requests
    6.7 The RESTful Way: XML/HTTP
    6.8 SOAP Web Services
    6.9 Working With Responses
    Hack 76 View XML Responses in a Browser
    Hack 77 Embed Product Details into a Web Page with PHP
    Hack 78 Program AWS with PHP
    Hack 79 Program AWS with Python
    Hack 80 Program AWS with Perl
    Hack 81 Loop Around the 10-Result Limit
    Hack 82 Program XML/HTTP with VBScript
    Hack 83 Transform AWS Results to HTML with XSLT
    Hack 84 Work Around Products Without Images
    Hack 85 Syndicate a List of Books with RSS
    Hack 86 Import Data Directly into Excel
    Hack 87 Program AWS with SOAP and VB.NET
    Hack 88 Program AWS with SOAP::Lite and Perl
    Hack 89 Program AWS with NuSOAP and PHP
    Hack 90 Create a Wireless Wish List
    Hack 91 Make Product Titles Shorter
    Hack 92 Encode Text for URLs
    Hack 93 Cache Amazon Images Locally
    Hack 94 Cache AWS Responses Locally
    Hack 95 Create an Amazon AIM Bot
    Hack 96 Compare International Sales
    Hack 97 Program AWS with Mozilla
    Hack 98 Search or Browse Amazon with Watson
    Hack 99 Add Cover Art to Your Digital Music Collection
    Hack 100 Using All Consuming's SOAP and REST Interfaces
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele