Hack92.Create a New Interface for Amazon.com


Hack 92. Create a New Interface for Amazon.com

Use Amazon.com's Web Services API to create a new search mechanism for books.

Amazon.com was one of the first major dotcom companies to embrace web services fully. Its API is both extensive and easy to use. For this hack, I'll use just the book search portion of the API to create a search mechanism that shows the results from two searches simultaneously in two columns. Any books that show up in both searches are placed at the top of the page across the two columns. Theoretically, this should show you the highly ranked books that cover both of the topics you are interested in.

9.8.1. The Code

Save the code in Example 9-11 as index.php.

Example 9-11. An HTML interface that compares two Amazon book searches
 <?php require_once 'PEAR.php'; require_once 'Services/Amazon.php'; $devtoken = "XXXXXXX"; $userid = "USERID"; $amazon = &new Services_Amazon( $devtoken, $userid ); function list_products( $products ) { ?> <table width="100%" cellspacing="0" cellpadding="3" border="0"> <?php foreach( $products as $product ) { ?> <tr> <td valign="top" width="10%" valign="middle" align="center"> <a href="<?php echo($product['url']); ?>" target="_new"><img src="/books/1/201/1/html/2/<?php echo($product['imagesmall']); ?>" border="0" /></a> </td> <td valign="top" width="90%"> <div ><a href="<?php echo($product['url']); ?>" target="_new"><?php echo( $product['name'] ); ?></a></div> <div ><?php echo($product['creator']); ?></div> <div >Release Date: <?php echo($product['release']); ?></div> <div >Price: <?php echo($product['price']); ?></div> </td> </tr> <?php } ?> </table> <?php } function find_books( $keyword )  {    global $amazon;   $out = array();   $products = $amazon->searchKeyword($keyword, "books", 1);   if( $products != null )   {     foreach($products as $product) {       $creator = 'by ' . implode(', ', $product['authors']);   $price = '';   if( $product['listprice'] != $product['ourprice'] )          $price = '<strike>'.$product['listprice'].'</strike> '.             $product['ourprice'];       else          $price = $product['listprice'];       if ( strlen( $product['name'] ) > 0 )         $out[ $product['asin'] ] = array(           'url' => $product['url'],   'imagesmall' => $product['imagesmall'],   'name' => $product['name'],   'release' => $product['release'],   'manufacturer' => $product['manufacturer'],   'asin' => $product['asin'],   'creator' => $creator,   'price' => $price         );       }     } return $out; } $terma = isset( $_GET['terma'] ) ? $_GET['terma'] : 'mysql';  $termb = isset( $_GET['termb'] ) ? $_GET['termb'] : 'php'; $lista = find_books( $terma );  $listb = find_books( $termb ); $overlaps = array();  $onlya = array();  $onlyb = array(); foreach( array_keys( $lista ) as $asin )  {    if ( array_key_exists( $asin, $listb ) )      $overlaps[ $asin ] = $lista[$asin];    else     $onlya[ $asin ] = $lista[$asin];  }  foreach( array_keys( $listb ) as $asin )  {   if ( !array_key_exists( $asin, $lista ) )      $onlyb[ $asin ] = $listb[$asin];  } ?> <html> <head> <title>Amazon Search Compare</title> <style type="text/css"> th { border-bottom: 1px solid black; background: #eee; margin-bottom: 5px; font- size: small; } td { font-size: small; } .title { font-weight: bold; font-size: medium; } .author { margin-left: 30px; font-style: italic; } </style> </head> <body> <form> <div style="width:600px;"> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr><th width="50%"><input type="text" name="terma" value="<?php echo( $terma ); ?>" /></th> <th width="50%"><input type="text" name="termb" value="<?php echo( $termb ); ?>" /><input type="submit" value="Go" /></th></tr> </table> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr><td><?php list_products( array_values( $overlaps ) ); ?></td></tr> </table> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr><td valign="top" width="50%"><?php list_products( array_values( $onlya ) ); ?></td> <td valign="top" width="50%"><?php list_products( array_values( $onlyb ) ); ?></ td></tr> </table> </div> </form> </body> </html> 

This hack uses the Services_Amazon PEAR module to search on two separate terms simultaneously. The find_books( ) function takes a search term and returns all of the books associated with that term. The script creates two of these lists, $lista and $listb, with each of the search results. It then creates three additional lists; those books that appear in both lists are stored in $overlaps, while those that are only in $lista go into $onlya and those only in $listb end up in $onlyb. The rest of the script formats these lists into HTML using standard PHP text templating techniques.

9.8.2. Running the Hack

Edit the code to replace the $devtoken and $userid values with those you get when signing up for Amazon Web Services access ( http://amazon.com/webservices). Next, install the Services_Amazon PEAR module [Hack #2].

The last step is to upload the index.php page to your web site, and navigate to it in your browser. This should give you a page that looks like Figure 9-14.

You can change the terms used in the search by altering the values in the column headings and clicking the Go button.

Figure 9-14. Simultaneously searching for PHP and MySQL


9.8.3. See Also

  • "Create a Weather Showdown" [Hack #100]

  • "Search Google by Link Graph" [Hack #91]



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net