HomePHP Page 7 - Using Amazon Web Services With PHP And SOAP (part 1)
Turning The Pages - 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.
You'll remember, from my explanation of the various arguments to BrowseNodeSearchRequest() a few pages back, that AWS returns search results in chunks of ten, and the "page" argument must be used to obtain subsequent pages of the result set.
Thus far, all the examples you've seen have been limited to displaying ten items...not very useful in the real world at all. That's why this next example adds previous and next page links to assist in navigating between the different pages of the result set.
<?php
// include class
include("nusoap.php");
// create a instance of the SOAP client object
$soapclient = new
soapclient("http://soap.amazon.com/schemas2/AmazonWebServices.wsdl",
true);
// uncomment the next line to see debug messages
// $soapclient->debug_flag = 1;
// if no page specified, start with page 1
if (!$_GET['page']) { $page = 1; } else { $page = $_GET['page']; }
// 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' => $page,
'mode' => 'books',
'tag' => 'melonfire-20',
'type' => 'lite',
'devtag' => 'YOUR-TOKEN-HERE'
);
// invoke the method
$result = $proxy->BrowseNodeSearchRequest($params);
$total = $result['TotalResults'];
$items = $result['Details'];
// format and display the results
?>
<html>
<head>
<basefont face="Verdana">
</head>
<body bgcolor="white">
<p> <p>
<table width="100%" cellspacing="0" cellpadding="5">
<tr>
<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>
</table>
<p>
Browse the catalog below, or search for a specific title.
<p>
<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)
{
?>
<tr>
<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>
<!-- next and prev page links -->
<?
$pageCount = ceil($total/10);
?>
<table width="100%" cellspacing="0" cellpadding="5">
<tr>
<td align="left">
<?
if ($page != 1)
{
?>
<a href="<? echo $_SERVER['PHP_SELF']; ?>?page=<? echo $page-1;
?>">Previous page</a> <? } ?> </td> <td align="center">Page <?
echo $page; ?> of <? echo $pageCount; ?></td> <td align="right">
<? if ($page < $pageCount) { ?> <a href="<? echo $_SERVER['PHP_SELF'];
?>?page=<? echo $page+1; ?>">Next page</a> <? } ?> </td> </tr> </table>
<font size="-1">
Disclaimer: All product data on this page belongs to Amazon.com. No
guarantees are made as to accuracy of prices and information. YMMV!
</font>
</body>
</html>
How does this work? It's actually pretty simple - first, the
total number of items in the result set is obtained from the SOAP response and assigned to a variable; this number is then divided by ten and rounded up to obtain the total number of pages to be displayed. Then, previous and next page links are added to the bottom of the page - each link calls the same script again and passes it a new page number via the GET method. This page number is then incorporated into the call to BrowseNodeSearchRequest(), and a new data set is obtained and displayed.
Here's what it looks like:
One caveat, though: AWS 2.0 contains a bug that sometimes causes it to display an incorrect number of total results. Hopefully, this will be fixed in an upcoming release - until then, be warned.