13.09.2016 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

The DEVTAG constant holds the value of the developer token Amazon gives you when<br />

you sign up.You need to change this value to your own developer token; otherwise, the<br />

application will not work.You can sign up for a tag at http://aws.amazon.com.<br />

Now let’s look back at index.php. It contains some preliminaries <strong>and</strong> then the main<br />

event loop.You begin by extracting any incoming variables from the $_REQUEST superglobal<br />

that came via GET or POST.You then set up some default values for some st<strong>and</strong>ard<br />

global variables that determine what will be displayed later, as follows:<br />

// default values for global variables<br />

if($mode=='') {<br />

$mode = 'Books'; // No other modes have been tested<br />

}<br />

if($browseNode=='') {<br />

$browseNode = 53; //53 is bestselling non-fiction books<br />

}<br />

if($page=='') {<br />

$page = 1; // First Page - there are 10 items per page<br />

You set the mode variable to Books. Amazon supports many other modes (types of products),<br />

but for this application, you just need to worry about books. Modifying the code<br />

in this chapter to deal with other categories should not be too hard.The first step in this<br />

expansion would be to reset $mode.You would need to check the Amazon documentation<br />

to see what other attributes are returned for nonbook products <strong>and</strong> remove bookspecific<br />

language from the user interface.<br />

The browseNode variable specifies what category of books you would like displayed.<br />

This variable may be set if the user has clicked through one of the Selected Categories<br />

links. If it is not set—for example, when the user first enters the site—you will set it to<br />

53. Amazon’s browse nodes are simply integers that identify a category.The value 53 represents<br />

the category Non-Fiction Books, which seems as good a node as any other to<br />

display on the initial front page given that some of the best generic categories (such as<br />

Best Sellers) are not available as browse nodes.<br />

The page variable tells Amazon which subset of the results you would like displayed<br />

within a given category. Page 1 contains results 1–10, page 2 has results 11–20, <strong>and</strong> so<br />

on. Amazon sets the number of items on a page, so you do not have control over this<br />

number.You could, of course, display two or more Amazon “pages” of data on one of<br />

your pages, but 10 is both a reasonable figure <strong>and</strong> the path of least resistance.<br />

Next, you need to tidy up any input data you have received, whether through the<br />

search box or via GET or POST parameters:<br />

//validate/strip input<br />

if(!eregi('^[A-Z0-9]+$', $ASIN)) {<br />

// ASINS must be alpha-numeric<br />

$ASIN ='';<br />

}

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

Saved successfully!

Ooh no, something went wrong!