13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

814 Chapter 33 Connecting to <strong>Web</strong> Services with XML <strong>and</strong> SOAP<br />

When you register for a developer token, you need to agree to the license agreement.<br />

This is worth reading because it is not the usual yada-yada software license. Some of the<br />

license conditions that are important during implementation are the following:<br />

n You must not make more than one request per second.<br />

n You must cache data coming from Amazon.<br />

n You may cache most data for 24 hours <strong>and</strong> some stable attributes for up to three<br />

months.<br />

n If you cache prices <strong>and</strong> availability for more than an hour, you must display a disclaimer.<br />

n You must link back to a page on Amazon.com <strong>and</strong> must not link text or graphics<br />

downloaded from Amazon to another commercial website.<br />

With a hard-to-spell domain name, no promotion, <strong>and</strong> no obvious reason to use<br />

Tahuayo.com instead of going straight to Amazon.com, you do not need to take any further<br />

steps to keep requests below one per second.<br />

In this project, you implement caching to meet the conditions at points 2 to 4.The<br />

application caches images for 24 hours <strong>and</strong> product data (which contains prices <strong>and</strong><br />

availability) for 1 hour.<br />

Your application also follows the fifth point.You want items on the main page to link<br />

to detailed pages on your site, but you link to Amazon when an order is complete.<br />

Parsing XML: REST Responses<br />

The most popular interface Amazon offers to its <strong>Web</strong> Services is via REST.This interface<br />

accepts a normal HTTP request <strong>and</strong> returns an XML document.To use this interface,<br />

you need to be able to parse the XML response Amazon sends back to you.You can do<br />

this by using <strong>PHP</strong>’s SimpleXML library.<br />

Using SOAP with <strong>PHP</strong><br />

The other interface offering the same <strong>Web</strong> Services is SOAP.To access these services<br />

using SOAP, you need to use one of the various <strong>PHP</strong> SOAP libraries.There is a built-in<br />

SOAP library, but because it will not always be available, you can use the NuSOAP<br />

library. Because NuSOAP is written in <strong>PHP</strong>, it does not need compiling. It is just a<br />

single file to be called via require_once().<br />

NuSOAP is available from http://sourceforge.net/projects/nusoap/. NuSOAP is<br />

available under the Lesser GPL; that is, you may use it in any application, including nonfree<br />

applications.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!