HomePHP Page 6 - Using Amazon Web Services With PHP And SOAP (part 1)
Sorting Things Out - 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.
Now, the list displayed on the previous page is sorted in the default order imposed by Amazon.com. However, AWS allow you to alter this sort order by specifying an optional "sort" argument in the call to BrowseNodeSearchRequest(). This "sort" argument allows you to sort products by price, by sales rank, by rating, by date or alphabetically.
In order to demonstrate this, consider the following enhancement to the example on the previous page, which performs three BrowseNodeSearchRequest() calls, each one applying a different sort criteria. The first one displays items in the default order; the second displays featured items first; and the third displays items by sales rank. Notice how the results of these three AWS calls can be massaged to create a more dynamic, informative and user-friendly page.
<?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;
// create a proxy so that WSDL methods can be accessed directly
$proxy = $soapclient->getProxy();
// get items from the catalog
// sort order is default
$catalogParams = array(
'browse_node' => 18,
'page' => 1,
'mode' => 'books',
'tag' => 'melonfire-20',
'type' => 'lite',
'devtag' => 'YOUR-TOKEN-HERE'
);
$catalogResult = $proxy->BrowseNodeSearchRequest($catalogParams);
$catalogTotal = $catalogResult['TotalResults'];
$catalogItems = $catalogResult['Details'];
// get today's featured items
// sort order is by featured items
$featuredParams = array(
'browse_node' => 18,
'page' => 1,
'mode' => 'books',
'tag' => 'melonfire-20',
'type' => 'lite',
'sort' => '+pmrank',
'devtag' => 'YOUR-TOKEN-HERE'
);
$featuredResult = $proxy->BrowseNodeSearchRequest($featuredParams);
$featuredTotal = $featuredResult['TotalResults']; $featuredItems =
$featuredResult['Details'];
// get bestsellers
// sort order is by sales ranking
$bestsellersParams = array(
'browse_node' => 18,
'page' => 1,
'mode' => 'books',
'tag' => 'melonfire-20',
'type' => 'lite',
'sort' => '+salesrank',
'devtag' => 'YOUR-TOKEN-HERE'
);
$bestsellersResult =
$proxy->BrowseNodeSearchRequest($bestsellersParams);
$bestsellersTotal = $bestsellersResult['TotalResults'];
$bestsellersItems = $bestsellersResult['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>
<!-- outer table -->
<table width="100%" cellspacing="0" cellpadding="5">
<tr>
<td align="left" valign="top" rowspan="2" width="65%">
<!-- inner catalog table -->
<table border="0" cellspacing="5" cellpadding="0">
<?
// parse the $items[] array and extract the necessary
information
// (image, price, title, author, item URL)
foreach ($catalogItems 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>
</td>
<td valign="top">
<font size="-1">
<!-- featured item table -->
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<td>
<b><font size="-1">Today's Featured Items:</font></b>
<ul>
<?
for ($x=0; $x<5; $x++)
{
$f = $featuredItems[$x];
?>
<li><i><a href="<? echo $f['Url']; ?>"><font
size="-1"><b><? echo $f['ProductName']; ?></b> - <? echo implode(", ",
$f['Authors']); ?> (<? echo $f['OurPrice']; ?>)</font></a></i>
<p>
<?
}
?>
</ul>
</td>
</tr>
</table>
<p>
<!-- bestseller list -->
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<td>
<b><font size="-1">Bestsellers:</font></b>
<ul>
<?
for ($y=0; $y<5; $y++)
{
$b = $bestsellersItems[$y];
?>
<li><i><a href="<? echo $b['Url']; ?>"><font
size="-1"><b><? echo $b['ProductName']; ?></b> - <? echo implode(", ",
$b['Authors']); ?> (<? echo $b['OurPrice']; ?>)</font></a></i>
<p>
<?
}
?>
</ul>
</td>
</tr>
</table>
</font>
</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>
In this case, the additional "sort" argument is used to
obtain a list of featured items and bestsellers within the Mystery node of the Amazon book database. Here's what the output looks like:
A number of other sort criteria are available in AWS - take a look at the documentation for details.