Search for Keywords at Amazon.com
Now we will search for books by querying AWS
for keywords "web services"
Querying AWS
Since we will be displaying information on all 10 products (we are assuming
keywords "web services"
will match more than 10 products in the
Amazon catalog), we probably won't be showing much at a time. In fact we will limit
ourselves to a small image, the books' titles, their authors, the price, and of course,
a button to add the books to the Amazon shopping cart. For this we use
liteQuery(), which was discussed
in this document.
As $keywords
we will be using "web services"
,
as $serachType
we will be using "Keywords". We will set
$mode
to "books", beacause this is what we are interested in, and
omit the other two parameters because their default values are fine for now.
- //setAmazoopConfig('check_alternative_images', IMAGE_AS_IS | IMAGE_ALTERNATIVES_4, 'test');
- // 2. Create new AmazonQuery
-
- $aq = new AmazonQuery();
Note: liteQuery() is
only available as of version 0.3RC1 of amazOOP, before this, you should use the
lower-level function query() to query
AWS.
Once you have queried AWS
you should, as in the previous examples (1,
2), check for errors
before displaying the information obtained.
Displaying information
As in previous examples (1, 2),
a ProductInfo object will be returned after querying
AWS.
We hadn't talked about this object before, because it wasn't very important in our
previous searches. In this occassion, however, we will be using methods from the
ProductInfo class.
General information about the request
First, we want to know how many results actually match our keywords. For this we will
use getTotalResults():
- echo "It looks like there's an error: " . $error;
Once we know that, we want to know out of the potentially thousands of results, which
ones we are displaying. For this we use resultsLow() and resultsHigh():
Finally, we want to know what page we are showing, and how many pages there are in
total.
You're supposed to know which page you are displaying, since it was you who
explicitly requested it. Nevertheless, if you forgot, you can always check what
parameters you sent to AWS
with method requestArgument().
All you have to do is ask what "PAGE"
, you asked for:
It is Amazon's job, however, to tell you how many pages of results match your query.
To view this information, you use getTotalPages():
Displaying product information
The syntax to display information about multiple results is similar to the one
used to display multiple features, or multiple customer review, which was described
in a previous document.
All you have to do is use the method getDetails() and a for
loop, as shown below:
- echo "Showing results " . $queryResult->resultsLow();
You start the iteration at $i = 1
, because calling
getDetails(0) returns a random product and not the first product in the list.
We also know that getDetails()
will return false
if the requested product does not exist.
Therefore, we will use that as a condition to stop. Finally, we increment
$i
and use the retrieved product (which was assigned to
$product
in the for
loop) to display its
information.
If an assignment inside the for
loop confuses you, you can
also use the following syntax:
- for($i = 1; $queryResult->getDetails($i); $i++){
- $product = $queryResult->getDetails($i);
- echo $product->getImage('small', true, 'align="left"') . "\n";
- ...
This is what the loop looks like when we add the code to display the product's
information:
- echo "Showing results " . $queryResult->resultsLow();
- echo " through " . $queryResult->resultsHigh() . "<br>\n";
- echo "Showing page " . $queryResult->requestArgument("PAGE");
- echo " out of " . $queryResult->getTotalPages() . "</p>\n";
-
-
- for($i = 1; $product = $queryResult->getDetails($i); $i++){
The complete example
This is the source for the complete example, which should run if you modify line
5 to point to the actual path of aq.php
:
- <?php
- $start_time = explode(" ", microtime());
- $start_time = $start_time[1] + $start_time[0];
- // 1. Include AmazOOP in your page
- // You may need to change this to the actual physical path to the file
-
- require_once('../aq.php');
-
- setAmazoopConfig('address', 'cvswork', 'test');
- //setAmazoopConfig('check_alternative_images', IMAGE_AS_IS | IMAGE_NO_SIZE | IMAGE_ALTERNATIVES_4, 'test');
- setAmazoopConfig('check_alternative_images', IMAGE_NO_SIZE | IMAGE_ALTERNATIVES_3, 'test');
- //setAmazoopConfig('check_alternative_images', IMAGE_AS_IS | IMAGE_ALTERNATIVES_4, 'test');
- // 2. Create new AmazonQuery
-
- $aq = new AmazonQuery();
-
- // 3. Query Amazon for data:
- // We will request the books related to keywords "web services"
-
- $queryResult = $aq->liteQuery("chips", "Keywords", "everything");
-
-
- // 4. Check for errors
- if( $error = $queryResult->getErrorMessage() )
- echo "It looks like there's an error: " . $error;
-
- // If everything's alright
- else{
-
- // 5. Display the data you got from Amazon.
- echo "<p>Total Results: " . $queryResult->getTotalResults() . "<br>\n";
- echo "Showing results " . $queryResult->resultsLow();
- echo " through " . $queryResult->resultsHigh() . "<br>\n";
- echo "Showing page " . $queryResult->requestArgument("PAGE");
- echo " out of " . $queryResult->getTotalPages() . "</p>\n";
-
-
- for($i = 1; $product = $queryResult->getDetails($i); $i++){
- echo $product->getImage('small', true, 'align="left"') . "\n";
- echo $product->getURL('name') . "<br>\n";
- echo $product->getAuthors(2) . "<br>\n";
- echo "Our price: " . $product->getOurPrice() . "<br>\n";
- echo $product->buyNow() . "<br>";
- }
- }
-
- $end_time = explode (" ", microtime());
- $end_time = $end_time[1] + $end_time[0];
-
- echo "Page generated in " . number_format(1000*($end_time - $start_time), 2) . " milliseconds";
- ?>
The interpreted result
And this is the interpreted result:
eBay for Dummies
Marsha Collier
Our price: $15.39
Next Generation Application Integration: From Simple Information to Web Services
David S. Linthicum
Our price: $39.99
Microsoft Internet Information Services (IIS) 6.0 Resource Kit
The Microsoft Iis Team, Microsoft Corporation
Our price: $47.59
Active Directory for Dummies
Marcia Loughry, Marcia R. Loughry
Our price: $20.40
Microsoft .NET Distributed Applications: Integrating XML Web Services and .NET Remoting
Matthew Macdonald
Our price: $41.99
MCAD/MCSD Training Guide (70-320): Developing XML Web Services and Server Components with Visual C# .NET and the .NET Framework
Amit Kalani, Priti Kalani
Our price: $33.99
MCAD/MCSD Self-Paced Training Kit: Developing XML Web Services and Server Components with Microsoft Visual Basic .NET and Microsoft Visual C# .NET
Microsoft Corporation, Microsoft Corporation
Our price: $69.99
Developing Java Web Services: Architecting and Developing Secure Web Services Using Java
Ramesh Nagappan, Robert Skoczylas, et al.
Our price: $35.00
Web Services and Service-Oriented Architectures: The Savvy Manager's Guide
Douglas K. Barry
Our price: $23.77
Understanding Web Services: XML, WSDL, SOAP, and UDDI
Eric Newcomer
Our price: $27.99