1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP and MySQL Web Development - P162 potx

5 210 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 64,56 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

It also means that our display, cache, and manipulation functions do not need to know what format the data is stored in or where it originated.. Having gone through all this processing t

Trang 1

them as they come It also means that our display, cache, and manipulation functions do not need to know what format the data is stored in or where it originated

Having gone through all this processing to retrieve the data, we now return control back to the getARS()function and hence back to showBrowseNode().The next step is

showSummary($ars->products(), $page,

$ars->totalResults(), $mode,

$browseNode);

The showSummary()function simply displays the data in the AmazonResultSet, as we see it all the way back in Figure 31.1.We have not therefore included the function here

Using SOAP

Let’s go back and look at the SOAP version of the browseNodeSearch()function.We’ll repeat this section of the code here:

error_reporting(E_ALL & ~E_NOTICE);

$soapclient = new soapclient(

'http://soap.amazon.com/schemas2/AmazonWebServices.wsdl','wsdl');

$soap_proxy = $soapclient->getProxy();

$parameters['mode']=$mode;

$parameters['page']=$page;

$parameters['type']='heavy';

$parameters['tag']=$this->_assocID;

$parameters['devtag']=$this->_devTag;

$parameters['sort']='+salesrank';

$parameters['browse_node'] = $browseNode;

$result = $soap_proxy->BrowseNodeSearchRequest($parameters);

if(isSOAPError($result)) return false;

$this->_totalResults = $result['TotalResults'];

$counter = 0;

foreach($result['Details'] as $product) {

$this->_products[$counter] = new Product;

$this->_products[$counter]->soap = $result['Details'][$counter];

$counter++;

} unset($soapclient);

unset($soap_proxy) ;

There are no extra functions to go through here—the SOAP client does everything for us

Trang 2

You might not have control over the error reporting setting on your server If notices are configured to be reported to the browser, the NuSOAP library produces notices, so we’ve turned it off for now

We begin by creating an instance of the SOAP client:

$soapclient = new soapclient(

'http://soap.amazon.com/schemas2/AmazonWebServices.wsdl', 'wsdl');

We are providing the client with two parameters.The first is the WSDL description of the service, and the second parameter is to tell the SOAP client that this is a WSDL URL Alternatively, we could just provide one parameter: the endpoint of the service, which is the direct URL of the SOAP Server

We have chosen to do it this way for a good reason, which is right there in the next line of code:

$soap_proxy = $soapclient->getProxy();

This line creates a class according to the information in the WSDL document.This class, the SOAP proxy, will have methods that correspond to the methods of the Web Service This makes life much easier.We can interact with the Web Service as though it were a local PHP class

Next, we set up an array of the parameters we need to pass to the browsenodequery:

$parameters['mode']=$mode;

$parameters['page']=$page;

$parameters['type']='heavy';

$parameters['tag']=$this->_assocID;

$parameters['devtag']=$this->_devTag;

$parameters['sort']='+salesrank';

$parameters['browse_node'] = $browseNode;

Using the proxy class, we can then just call the Web Service methods, passing in the array of parameters:

$result = $soap_proxy->BrowseNodeSearchRequest($parameters);

The data stored in $resultis an array that we can directly store in our _productsarray

in the AmazonResultSetclass

That’s the SOAP version As you can see, it’s a great deal shorter than the XML version, and therefore preferable But hopefully you’ve learned something about parsing XML along the way

Caching the Data

Let’s go back to the getARS()function and talk about caching As you might recall, the function looks like this:

Trang 3

function getARS($type, $parameters) {

$cache = cached($type, $parameters);

if($cache) // if found in cache {

return $cache;

} else {

$ars = new AmazonResultSet;

if($type == 'asin')

$ars->ASINSearch(padASIN($parameters['asin']), $parameters['mode']);

if($type == 'browse')

$ars->browseNodeSearch($parameters['browsenode'],

$parameters['page'], $parameters['mode']);

if($type == 'search')

$ars->keywordSearch($parameters['search'], $parameters['page'],

$parameters['mode']);

cache($type, $parameters, $ars);

} return $ars;

}

All the application’s SOAP or XML caching is done via this function.We also have another to cache images.We begin by calling the cached()function to see whether the required AmazonResultSetis already cached If it is, we return that data instead of mak-ing a new request to Amazon:

$cache = cached($type, $parameters);

if($cache) // if found in cache {

return $cache;

}

If not, when we get the data back from Amazon, we add it to the cache:

cache($type, $parameters, $ars);

Let’s look at these two functions:cached()and cache().They are shown in Listing 31.13.These functions implement the caching Amazon requires as part of their terms and conditions

Listing 31.13 Caching Functions from cachefunctions.php

// check if Amazon data is in the cache // if it is, return it

// if not, return false function cached($type, $parameters)

Trang 4

if($type == 'browse')

$filename = CACHE.'/browse.'.$parameters['browsenode'].'.

'.$parameters['page'].'.'.$parameters['mode'].'.dat'; if($type == 'search')

$filename = CACHE.'/search.'.$parameters['search'].'.

'.$parameters['page'].'.'.$parameters['mode'].'.dat'; if($type == 'asin')

$filename = CACHE.'/asin.'.$parameters['asin'].'

.'.$parameters['mode'].'.dat';

// is cached data missing or > 1 hour old?

if(!file_exists($filename) ||

((mktime() - filemtime($filename)) > 60*60)) {

return false;

}

$data = file($filename);

$data = join($data, '');

return unserialize($data);

}

// add Amazon data to the cache function cache($type, $parameters, $data) {

if($type == 'browse')

$filename = CACHE.'/browse.'.$parameters['browsenode'].'.'

.$parameters['page'].'.'.$parameters['mode'].'.dat'; if($type == 'search')

$filename = CACHE.'/search.'.$parameters['search'].'.'

.$parameters['page'].'.'.$parameters['mode'].'.dat'; if($type == 'asin')

$filename = CACHE.'/asin.'.$parameters['asin'].'.' $parameters['mode'].'.dat';

$data = serialize($data);

$fp = fopen($filename, 'wb');

if(!$fp||(fwrite($fp, $data)==-1) ) {

echo ('<p>Error, could not store cache file</p>');

} fclose($fp);

}

Listing 31.13 Continued

Trang 5

Looking through this code, you can see that cache files are stored under a file name that consists of the type of query followed by the query parameters.The cache()function stores results by serializing them, and the cached()function deserializes them.The cached()function will also overwrite any data more than an hour old, as per the terms and conditions

The function serialize()turns stored program data into a string that can be stored

In this case, we are creating a storable representation of an AmazonResultSet object.

Calling unserialize()does the opposite, turning the stored version back into a data structure in memory Note that unserializing an object like this means you need to have the class definition in the file so that the class is comprehendible and useable once reloaded

In our application, retrieving a resultset from the cache takes a fraction of a second

Making a new live query takes up to ten seconds

Building the Shopping Cart

So, given all these amazing Amazon querying abilities, what can we do with them? The most obvious thing we can build is a shopping cart Because we’ve already covered this topic extensively in Chapter 25, “Building a Shopping Cart,” we will not go into deep detail here

The shopping cart functions are shown in Listing 31.14

Listing 31.14 cartfunctions.php—Implement the Shopping Cart

<?php require_once('AmazonResultSet.php');

// Using the function showSummary() in the file bookdisplay.php display // the current contents of the shopping cart

function showCart($cart, $mode) {

// build an array to pass

$products = array();

foreach($cart as $ASIN=>$product) {

$ars = getARS('asin', array('asin'=>$ASIN, 'mode'=>$mode));

if($ars)

$products[] = $ars->getProduct(0);

} // build the form to link to an Amazon.com shopping cart echo '<form method="POST"

action="http://www.amazon.com/o/dt/assoc/handle-buy-box">';

foreach($cart as $ASIN=>$product) {

$quantity = $cart[$ASIN]['quantity'];

Ngày đăng: 07/07/2014, 03:20