O'Reilly logo

PHP Hacks by Jack D. Herrington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

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="<?php echo($product['imagesmall']); ?>" border="0" /></a> </td> <td valign="top" width="90%"> <div class="title"><a href="<?php echo($product['url']); ?>" target="_new"><?php echo( $product['name'] ); ?></a></div> <div class="author"><?php echo($product['creator']); ?></div> <div class="date">Release Date: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required