765Chapter 41Web ServicesProject: A REST ClientListing 41-1 is (Web server certificate)

765Chapter 41Web ServicesProject: A REST ClientListing 41-1 is a basic client script for Amazon s elegantly simple REST service, which hasbeen available (with some changes) to Amazon Associates and other developers since springof 2002. You feed the script a search string at the top, and it outputs a CSS-formatted box atthe end containing information about the current edition of the book in question. This service clearly demonstrates the biggest advantage of REST: You can work with it by usingthe HTTP concepts and the PHP functions you re already familiar with. For all intents andpurposes, you are simply asking for a Web page by using http fopen(). It happens to be well- formed XML instead of HTML but that is incidental to the transport mechanism. We chose to parse the XML by using PHP s DOM XML extension, which so far has found rela- tively few real-world uses (see Chapter 40 for discussion of the DOM). Many other PHP-literateAmazon developers have produced scripts that use other types of parsing, such as string pars- ing and regex, to extract the desired information but we want to show you the power ofusing XML itself. We should warn you, however, that this type of solution does not scale DOM XML is a noto- rious memory hog. (We ve heard credible reports that a 1,000-line XML document read intothe DOM results in 1MB of memory being appropriated.) However, the Amazon Web servicesinterface will only return a few items at a time, so DOM XML is an appropriate technology forthis purpose. The DOM extension changed significantly in PHP5. This script will not work at all in versionsof PHP before 5.0.0b2. Obviously the script will also not work unless you have previouslycompiled PHP with the –with-domxmlflag and libxml2. Listing 41-1:Client for Amazon RESTservice (rest_amazon_client.php) From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Leave a Reply