HomePHP Page 5 - Using Amazon Web Services With PHP And SOAP (part 1)
The Bookworm Turns - PHP
It's the coolest store on the Web - and now, its databases are accessible to you. Welcome to Amazon Web Services, an XML-based API that allows you to quickly build an online store that leverages off Amazon.com's massive databases. Find out more, inside.
If you take a close look at the output of the previous example, you'll see that the call to BrowseNodeSearchRequest() results in a PHP associative array containing a series of result elements. It's extremely simple to convert the raw data contained within this array into a properly-formatted HTML page. Watch!
// include class
// create a instance of the SOAP client object
$soapclient = new
// uncomment the next line to see debug messages
// $soapclient->debug_flag = 1;
// create a proxy so that WSDL methods can be accessed directly
$proxy = $soapclient->getProxy();
// set up an array containing input parameters to be
// passed to the remote procedure
$params = array(
'browse_node' => 18,
'page' => 1,
'mode' => 'books',
'tag' => 'melonfire-20',
'type' => 'lite',
'devtag' => 'YOUR-TOKEN-HERE'
// invoke the method
$result = $proxy->BrowseNodeSearchRequest($params);
<? echo $result['faultstring'];?>
$total = $result['TotalResults'];
$items = $result['Details'];
// format and display the results
<table width="100%" cellspacing="0" cellpadding="5">
<td bgcolor="Navy"><font color="white" size="-1"><b>Welcome to
The Mystery Bookstore!</b></font></td>
<td bgcolor="Navy" align="right"><font color="white"
size="-1"><b><? echo date("d M Y", mktime());?></b></font></td> </tr>
Browse the catalog below, or search for a specific title.
<table width="100%" border="0" cellspacing="5" cellpadding="0"> <?
// parse the $items array and extract the necessary information
// (image, price, title, author, item URL)
foreach ($items as $i)
<td align="center" valign="top" rowspan="3"><a href="<? echo $i['Url'];
?>"><img border="0" src=<? echo $i['ImageUrlSmall']; ?>></a></td>
<td><font size="-1"><b><? echo $i['ProductName']; ?></b> / <? echo
implode(", ", $i['Authors']); ?></font></td> </tr> <tr> <td align="left"
valign="top"><font size="-1">List Price: <? echo $i['ListPrice']; ?> /
Amazon.com Price: <? echo $i['OurPrice']; ?></font></td> </tr> <tr> <td
align="left" valign="top"><font size="-1"><a href="<? echo $i['Url'];
?>">Read more about this title on Amazon.com</a></font></td> </tr> <tr>
<td colspan=2> </td> </tr> <? } ?> </table>
Disclaimer: All product data on this page belongs to Amazon.com. No
guarantees are made as to accuracy of prices and information. YMMV!
Here's what it looks like:
There's no magic here - all I've done is taken the associative array created by NuSOAP from the SOAP response and massaged its elements into a properly-laid out Web page. Most of the work happens in the "foreach" loop, which iterates through the result array and displays the items in a table, complete with thumbnail images.
In case you're wondering where all the data came from, flip back to the previous page and look at the keys of the associative array generated by NuSOAP - you'll see that the name of each key is self-explanatory, and maps into the data displayed on the page above. Notice also that each item is accompanied by a link to the product information page on the actual Amazon.com Web site, and that this URL (obtained from the "Url" key of the result array) also contains an embedded Associates ID so that Amazon can send you a commission in case the click results in an actual sale.
In the event that the procedure generates an error on the server, the response array will contain a SOAP fault. It's generally considered good programming practice to check for this and handle it appropriately - you'll see that I've done this in the script above.