amazOOP amazOOP
[ class tree: amazOOP ] [ index: amazOOP ] [ all elements ]
< Prev Up Next >
Making a "heavy" request, an example of an ASIN search Using amazOOP Other Search Types

Keyword search, looping through results

Look for a product using keywords and loop through the results returned by AWS

Mauricio Diaz Orlich
Copyright 2004, Mauricio Diaz
(amazOOP 0.3RC1)

Summary

By now you should have a, more or less, clear idea of how to query AWS for individual products. If you don't, you should read Making a "lite" request, an example of an ISBN search and Making a "heavy" request, an example of an ASIN search, or post to the Forums for help.

In this section we will cover Keyword searches, based on which you can retrieve results for Author seraches, Artist searches, and other types of queries that return more than one result.

About Keyword searches

I guess the only thing worth saying about Keyword searches, and any other type of searches, is that the query must be URL-encoded. This means that all characters that are not letters, numbers or - and _ should be replaced by % and an hexadecimal number. In amazOOP however, you don't have to worry about this. You should enter your search strings as normal text and amazOOP will convert them before submitting them to AWS.

Another important thing to remember is that AWS will return at the most 10 results per query. You have to use getTotalPages() to determine how many pages to have to fetch in order to have all the results. You'll read more about this below.

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.

  1. //setAmazoopConfig('check_alternative_images', IMAGE_AS_IS | IMAGE_ALTERNATIVES_4, 'test');
  2. // 2. Create new AmazonQuery
  3.  
  4. $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():

  1. 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:

    1. else{

    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:

      1. 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:

      1. for($i = 1; $queryResult->getDetails($i); $i++){
      2. $product = $queryResult->getDetails($i);
      3. echo $product->getImage('small', true, 'align="left"') . "\n";
      4. ...

      This is what the loop looks like when we add the code to display the product's information:

      1. echo "Showing results " . $queryResult->resultsLow();
      2. echo " through " . $queryResult->resultsHigh() . "<br>\n";
      3. echo "Showing page " . $queryResult->requestArgument("PAGE");
      4. echo " out of " . $queryResult->getTotalPages() . "</p>\n";
      5.  
      6.  
      7. 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:

      1. <?php
      2. $start_time = explode(" ", microtime());
      3. $start_time = $start_time[1] + $start_time[0];
      4. // 1. Include AmazOOP in your page
      5. // You may need to change this to the actual physical path to the file
      6.  
      7. require_once('../aq.php');
      8.  
      9. setAmazoopConfig('address', 'cvswork', 'test');
      10. //setAmazoopConfig('check_alternative_images', IMAGE_AS_IS | IMAGE_NO_SIZE | IMAGE_ALTERNATIVES_4, 'test');
      11. setAmazoopConfig('check_alternative_images', IMAGE_NO_SIZE | IMAGE_ALTERNATIVES_3, 'test');
      12. //setAmazoopConfig('check_alternative_images', IMAGE_AS_IS | IMAGE_ALTERNATIVES_4, 'test');
      13. // 2. Create new AmazonQuery
      14.  
      15. $aq = new AmazonQuery();
      16.  
      17. // 3. Query Amazon for data:
      18. // We will request the books related to keywords "web services"
      19.  
      20. $queryResult = $aq->liteQuery("chips", "Keywords", "everything");
      21.  
      22.  
      23. // 4. Check for errors
      24. if( $error = $queryResult->getErrorMessage() )
      25. echo "It looks like there's an error: " . $error;
      26.  
      27. // If everything's alright
      28. else{
      29.  
      30. // 5. Display the data you got from Amazon.
      31. echo "<p>Total Results: " . $queryResult->getTotalResults() . "<br>\n";
      32. echo "Showing results " . $queryResult->resultsLow();
      33. echo " through " . $queryResult->resultsHigh() . "<br>\n";
      34. echo "Showing page " . $queryResult->requestArgument("PAGE");
      35. echo " out of " . $queryResult->getTotalPages() . "</p>\n";
      36.  
      37.  
      38. for($i = 1; $product = $queryResult->getDetails($i); $i++){
      39. echo $product->getImage('small', true, 'align="left"') . "\n";
      40. echo $product->getURL('name') . "<br>\n";
      41. echo $product->getAuthors(2) . "<br>\n";
      42. echo "Our price: " . $product->getOurPrice() . "<br>\n";
      43. echo $product->buyNow() . "<br>";
      44. }
      45. }
      46.  
      47. $end_time = explode (" ", microtime());
      48. $end_time = $end_time[1] + $end_time[0];
      49.  
      50. echo "Page generated in " . number_format(1000*($end_time - $start_time), 2) . " milliseconds";
      51. ?>

      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


      < Prev Up Next >
      Making a "heavy" request, an example of an ASIN search Using amazOOP Other Search Types
       
      Documentation generated on Sat, 21 Aug 2004 17:39:44 +0200 by phpDocumentor 1.3.0RC3
      hosted by
      SourceForge.net Logo