HomePHP Page 4 - Using Amazon Web Services With PHP And SOAP (part 2)
Drilling Deeper - PHP
In this concluding article, find out how to add search features to your AWS-backed online store, and link your product pages up to Amazon.com's shopping carts and wish lists.
AWS also allows you to look for items in specific categories, supporting searches by author, actor, artist and manufacturer. Here's a list of the relevant methods:
AuthorSearchRequest() - search by author;
ArtistSearchRequest() - search by artist or musician;
ActorSearchRequest() - search by actor or actress;
DirectorSearchRequest() - search by director;
ManufacturerSearchRequest() - search by product manufacturer;
With methods like these, it's easy to build a more powerful search engine for your store. Take a look:
<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>Search</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>
<?
if (!$_POST['q'])
{
?>
Select
a category and enter a search term:
<form method="post" action="<? echo
$_SERVER['PHP_SELF']; ?>"> <select
name="type">
<option value="author">Author</option>
<option value="artist">Artist</option>
<option value="actor">Actor</option>
<option value="director">Director</option>
<option value="manufacturer">Manufacturer</option>
</select>
<input
type="text" name="q">
</form>
<?
}
else
{
$type = $_POST['type'];
// include
class
include("nusoap.php");
// create a instance of the SOAP client object
$soapclient
= new
soapclient("http://soap.amazon.com/schemas2/AmazonWebServices.wsdl",
true);
//
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
//
use a switch loop to define the search parameters for each type
switch ($type)
{
case
"author":
$mode = "books";
$func = "AuthorSearchRequest";
break;
case
"artist":
$mode = "music";
$func = "ArtistSearchRequest";
break;
case
"actor":
$mode = "vhs";
$func = "ActorSearchRequest";
break;
case "director":
$mode
= "vhs";
$func = "DirectorSearchRequest";
break;
case "manufacturer":
$mode
= "electronics";
$func = "ManufacturerSearchRequest";
break;
}
$params =
array(
$type => htmlentities($_POST['q']),
'page'
=> 1,
'mode' => $mode,
'tag' => 'melonfire-20',
'type' => 'lite',
'devtag' => 'YOUR-TOKEN-HERE'
);
// invoke
the method
$result = $proxy->$func($params);
$total = $result['TotalResults'];
$items
= $result['Details'];
// format and display the results
?>
Your search for <b><?
echo $_POST['q']; ?></b> returned <? echo $total;
?> matches.
<p>
<table
width="100%" border="0" cellspacing="5" cellpadding="0"> <?
// parse the $items[]
array and extract the necessary information
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> /
<?
if (is_array($i['Authors']))
{
echo implode(", ", $i['Authors']);
}
else if (is_array($i['Artists']))
{
echo implode(", ", $i['Artists']);
}
else if ($i['Manufacturer'])
{
echo
$i['Manufacturer'];
}?>
</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>
<?
}
?>
</body>
</html>
In this case, I've simply added a new item to my search form - a drop-down list
containing a list of the categories to search in. Each of these categories corresponds to one of the AWS methods listed above, and my PHP form processor uses a "switch" statement to call the appropriate method and pass it the search keywords. The result is then processed in the usual way, and formatted for display to the user.
Here are a couple of screenshots demonstrating how this works: