case 'LISTID' :$this->_currentProduct->listIDs[] = $cdata; break; case 'BROWSENAME' : @$this->_currentProduct->browseNames[$this->_currentProduct->_ currentBrowseName] .= $cdata; // fiel
Trang 1if($name=='DETAILS') {
$this->_currentProduct = new Product();
} if($name == 'BROWSENODE') {
$this->_currentProduct->_currentBrowseName++;
} if($name == 'CUSTOMERREVIEW') {
$this->_currentProduct->_currentReview++;
} }
// function to catch callbacks when the XML parser has data from // an element
function cdataHandler($parser, $cdata) {
$this->_currentName = array_slice($this->_names, -1, 1);
$this->_currentName = $this->_currentName[0] ;
switch($this->_currentName) {
case 'TOTALRESULTS' :
$this->_totalResults = $cdata;
break;
case 'DETAILS' : break;
case 'AUTHOR' :
$this->_currentProduct->authors[] = $cdata;
break;
case 'RATING' : case 'SUMMARY' : case 'COMMENT' :
@$this->_currentProduct->
customerReviews[$this->_currentProduct->_currentReview]
[$this->_currentName] = $cdata;
// fields that may contain returns and &s need to be concatenated // concatenation will give a notice if they are enabled -
// hence the @ break;
Listing 31.8 Continued
Trang 2case 'LISTID' :
$this->_currentProduct->listIDs[] = $cdata;
break;
case 'BROWSENAME' :
@$this->_currentProduct->browseNames[$this->_currentProduct->_
currentBrowseName] = $cdata;
// fields that may contain returns and &s need to be concatenated // concatenation will give a notice if they are enabled -
// hence the @ break;
case 'PRODUCT' :
$this->_currentProduct->similarProducts[] = $cdata;
break;
// there are certain keys we are dealing with the // children of separately so can ignore
case 'CUSTOMERREVIEW' : case 'AUTHORS' : case 'BROWSELIST' : case 'BROWSENODE' : case 'LISTS' : case 'REVIEWS' : case 'SIMILARPRODUCTS' : //do nothing
break;
default :
@$this->_currentProduct->nodes[$this->_currentName] = $cdata; break;
} }
// function to get callbacks when the XML parser reaches an end of element function endElementHandler($parser, $name)
{ if($name=='DETAILS') {
//these are no longer required unset($this->_currentProduct->_currentReview);
unset($this->_currentProduct->_currentBrowseName);
array_push($this->_products, $this->_currentProduct);
Listing 31.8 Continued
Trang 3} array_pop($this->_names);
} }
? >
This useful class does exactly the sort of thing classes are good for It encapsulates the interface to Amazon in a nice black box.Within the class the connection to Amazon can
be made either via the XML over HTTP method or the SOAP method.The method it will use is determined by the global METHODconstant we set at the very beginning.
Let’s begin by going back to the Category Search example.We use the AmazonResultSetclass as follows:
$ars = new AmazonResultSet;
$ars->browseNodeSearch($parameters['browsenode'],
$parameters['page'],
$parameters['mode']);
This class has no constructor, so we’ll go straight to that browseNodeSearch()method.
We are passing it three parameters: the browsenodenumber we are interested in (corre-sponding to, say, Business & Investing, or Computers & Internet); the page number, rep-resenting the records we would like retrieved; and the mode, reprep-resenting the type of merchandise we are interested in.The code for this method is shown excerpted in Listing 31.9.
Listing 31.9 browseNodeSearch() Method—Performing a Category Search
// Perform a query to get a page full of products from a browse node // Switch between XML/HTTP and SOAP in constants.php
// Returns an array of Products function browseNodeSearch($browseNode, $page, $mode) {
if(METHOD=='SOAP') {
// the NuSOAP class generates a lot of notices Turn them off
error_reporting(error_reporting() & ~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;
Listing 31.8 Continued
Trang 4$parameters['browse_node'] = $browseNode;
// perform actual soap query
$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);
} else { // form URL and call parseXML to download and parse it
$this->_type = 'browse';
$this->_browseNode = $browseNode;
$this->_page = $page;
$this->_mode = $mode;
$this->_url = 'http://xml.amazon.com/onca/xml2?t='.ASSOCIATEID
.'&dev-t='.DEVTAG.'&BrowseNodeSearch=' $this->_browseNode.'&mode='.$this->_mode '&type=heavy&page='.$this->_page.'&sort=+salesrank&f=xml';
$this->parseXML();
}
return $this->_products;
} Depending on the value of the METHODconstant, this method will either perform the query via XML over HTTP or via SOAP.We will look at each of these separately.
Using XML Over HTTP
We begin by setting a few important class member variables:
n type—The type of search required.We are searching for books within a particular browsenode, so we set the value to browse.
n browse—The value of the particular browsenodewe have been passed as a parameter.
Listing 31.9 Continued
Trang 5page—The page number that we have been passed as a parameter.
n mode—The type of items we are searching for (for example,books) that we have been passed as a parameter.
n url—The URL at Amazon that we need to connect to in order to perform this type of search.
The URLs that we make our HTTP connections to for different types of searches and the parameters they expect can be found in the Amazon.com Web Services API and Integration Guide in your developer’s kit Look closely at the GETparameters we are passing in here:
$this->_url = 'http://xml.amazon.com/onca/xml2?t='.ASSOCIATEID
.'&dev-t='.DEVTAG.'&BrowseNodeSearch=' $this->_browseNode.'&mode='.$this->_mode '&type=heavy&page='.$this->_page
.'&sort=+salesrank&f=xml';
The parameters we need to pass to this URL are as follows:
n t—Your Associate ID.
n dev-t—Your developer token.
n BrowseNodeSearch—The browsenodenumber you want to search.
n mode—books, or another valid product type.
n type—Heavyor lite(note spelling!) Heavy gives more information.
n page—Group of ten results.
n sort—The order we would like the results returned in.This is an optional parameter In this case, we have set it to +salesrankbecause we would like results
in sales rank order.
n f—The format.This should always contain the value 'xml' Valid sort types are as follows:
n Featured Items:+pmrank
n Bestselling:+salesrank
n Average Customer Review:+reviewrank
n Price (Low to High):+pricerank
n Price (High to Low):+inverse-pricerank
n Publication Date:+daterank
n Alphabetical (A-Z):+titlerank
n Alphabetical (Z-A):-titlerank