747 Solution Components We have implemented caching to meet the conditions at points 2 to 4.We cache images for 24 hours, and product data (which contains prices) for one hour. We have chosen to ignore point 5.We want items on the main page to link to detailed pages on our site, and only link to Amazon when complete.This is the way shopping cart applications usually work, and the way that sample third-party applications linked from Amazon’s site all work.This leads us to believe that Amazon is not terribly bothered by breaches of this condition. Feel free to make a different decision.An Amazon Web Services staffer indicated that they are aware of the situation, and may review the condition in the future. Parsing XML The first interface Amazon offers to its Web Services is via XML over HTTP.This inter- face accepts a normal HTTP request and returns an XML document.To use this inter- face, we will need to parse the XML response Amazon sends us.We will do this by using PHP’s XML library, which is based on the expat parser written by James Clark.This is the same XML parser used by Mozilla. It is a SAX parser. If you want to read more about the PHP XML library, you can look at the PHP Manual or the expat documenta- tion at http://www.jclark.com/xml/expat.html. Using SOAP with PHP The other interface offering the same Web Services is SOAP.To access them using SOAP, we will need to use one of the various PHP SOAP libraries. At the time of writ- ing, there were three notable PHP SOAP libraries: PHP-SOAP, PEAR SOAP, and NuSOAP. PHP-SOAP is a C extension to PHP, and will probably become the official SOAP extension. At the time of writing, it was still under development. PEAR SOAP is a set of PHP class files in PEAR that enable you to create SOAP clients and servers. NuSOAP is a set of PHP classes that also enable you to create SOAP clients and servers. We have decided to use NuSOAP, as it is the library that has the most documenta- tion. It also seems to be the library used by the majority of Amazon Web Services devel- opers using PHP, but this is probably at least in part because Amazon uses this library in its sample PHP code. NuSOAP is available from http://dietrich.ganx4.com/nusoap/. NuSOAP is avail- able under the Lesser GPL; that is, you may use it in non-free applications. Caching As we mentioned before, one of the terms and conditions imposed upon developers by Amazon is that data downloaded from Amazon via Web Services must be cached. In our solution, we will need to find a way to store and reuse the data that we download until it has passed its use-by date. 37 525x ch31 1/24/03 3:35 PM Page 747 748 Chapter 31 Connecting to Web Services with XML and SOAP Solution Overview For this project, we will again use an event-driven approach to writing our code, as we did in Chapters 27 and 28.We will not draw a system flow diagram in this example, as there are only a few screens in the system, and the links between them are simple. Users will begin at the main Tahuayo screen, shown in Figure 31.1. Figure 31.1 The first screen for Tahuayo shows all the main features of the site: category navigation, searching, and the shopping cart. As you can see, the main features of the site are the Selected Category display and the items in those categories. By default, we are displaying the Current Best Sellers category on the front page. If a user clicks on another category, she will see a similar display for that category. A brief piece of terminology before we go further:Amazon refers to categories as browse nodes.You will see this expression used throughout our code and the official docu- mentation. The documentation provides a partial list of popular browse nodes. In addition, if you want a particular one, you can browse the normal Amazon.com site and read it from the URL, but there is no way to get a complete list. Note that there are more books and links to additional pages at the bottom of this page that you can’t see in the screenshot.We are displaying 10 books on each page, along with links to up to 30 other pages.This 10-per page value is set by Amazon.The 30- page limit is our own arbitrary choice. 37 525x ch31 1/24/03 3:35 PM Page 748 749 Solution Overview From here, users can click through to detailed information on individual books.This screen is shown in Figure 31.2. Figure 31.2 Our details page shows more information about a particular book, including similar products and reviews. Although it does not all fit in a screenshot, we are showing most, but not all, of the information that Amazon sends with a heavy request on this page.We are choosing to ignore parts that are aimed at products other than books and the list of categories the book fits in. If users click through the cover image, they will be able to see a larger version of the image. You might have noticed the search box at the top of the screen in these figures.This search will run a keyword search through our site and search Amazon’s catalog via its Web Services interface.An example of the output of a search is shown in Figure 31.3. Although we only list a few categories, customers can get to any book through the search facility and navigating to particular books. Each individual book has an Add to Cart link with it. Clicking on this or the Details link in the cart summary takes us to a display of the cart contents.This is shown in Figure 31.4. 37 525x ch31 1/24/03 3:35 PM Page 749 750 Chapter 31 Connecting to Web Services with XML and SOAP Figure 31.3 The results of searching for aardman. Figure 31.4 From the shopping cart page, we can delete items, clear the cart, or check out. 37 525x ch31 1/24/03 3:35 PM Page 750 751 Solution Overview Finally, when a customer checks out by clicking on one of the Checkout links, we send the details of her shopping cart to Amazon and take her there. She will see a page similar to the one in Figure 31.5. Figure 31.5 The items that were in the customer’s Tahuayo cart are now in her Amazon cart. You should now understand what we mean by building our own front end and using Amazon as the back end. Because we have again used the event-driven approach, most of the core decision making logic of the application is in one file, index.php. An overview of the files in the application is shown in Table 31.1. Table 31.1 Files in the Tahuayo Application Filename Type Description index.php Application The main application file about.php Application Shows the About page constants.php Include file Sets up some global constants topbar.php Include file Generates the info bar across the top of each page and the CSS bottom.php Include file Generates the footer at the bottom of each page AmazonResultSet.php Class file Contains the PHP class that stores the result of each Amazon query 37 525x ch31 1/24/03 3:35 PM Page 751 . one of the various PHP SOAP libraries. At the time of writ- ing, there were three notable PHP SOAP libraries: PHP- SOAP, PEAR SOAP, and NuSOAP. PHP- SOAP is a C extension to PHP, and will probably. more about the PHP XML library, you can look at the PHP Manual or the expat documenta- tion at http://www.jclark.com/xml/expat.html. Using SOAP with PHP The other interface offering the same Web Services. under development. PEAR SOAP is a set of PHP class files in PEAR that enable you to create SOAP clients and servers. NuSOAP is a set of PHP classes that also enable you to create SOAP clients and