HomePHP Page 3 - Using Amazon Web Services With PHP And SOAP (part 2)
Searching For Words - 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.
You might remember this page from the first part of this article:
You might also remember that I never got around to adding a search box. Let's do that now:
<?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);
//
create a proxy so that WSDL methods can be accessed directly
$proxy = $soapclient->getProxy();
//
if no page specified, start with page 1
if (!$_GET['page']) { $page = 1; } else
{ $page = $_GET['page']; }
// if keyword available, use it
if ($_POST['keyword']
&& $_POST['keyword'] != "") { $keyword =
$_POST['keyword']; } if ($_GET['keyword']
&& $_GET['keyword'] != "") {
$keyword = $_GET['keyword']; }
// if a keyword
is present, do a keyword search
if ($keyword)
{
$params = array(
'keyword'
=> htmlentities($keyword),
'page' => $page,
'mode'
=> 'books',
'tag' => 'melonfire-20',
'sort'
=> '+pmrank',
'type' => 'lite',
'devtag' => 'YOUR-TOKEN-HERE'
);
//
invoke the method
$result = $proxy->KeywordSearchRequest($params);
}
else
{
//
else perform a node browse
$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>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Browse
the catalog below, or search for a specific title </td>
<td><br><form
method="post" action="<? echo $_SERVER['PHP_SELF'];
?>"><input type="text"
name="keyword"></form></td> </tr> </table>
<p>
<table width="100%"
border="0" cellspacing="5" cellpadding="0"> <?
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;
?>&keyword=<?
echo $keyword; ?>">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; ?>&keyword=<? echo
$keyword;
?>">Next page</a> <? } ?> </td> </tr> </table>
</body>
</html>
As you can see, this is pretty simple - I've added a small input box for the
user to enter a search term, and modified the form processor to check for a keyword and run a KeywordSearchRequest() if it's present, or a BrowseNodeSearchRequest() for the default node if it isn't. The next and previous page links also now pass an additional "keyword" parameter back and forth, so that the user can page through either the product catalog returned by BrowseNodeSearchRequest() or the result set returned by KeywordSearchRequest().
Here are a couple of screenshots demonstrating what the final result looks like: