Location: PHPKode > scripts > Search results retriever > class.amazonRetriever.php
<?php
/*
* Copyright (C) 2010 Pierre-Luc Germain (plger)
* See class.httpRetriever.php for more information.
*/

class amazonRetriever extends httpRetriever {
	
	public $public_key = false;
	public $private_key= false;
	public $server = 'com';		//which server's products? available: ca,de,fr,co.jp,co.uk,com
	
	public $allowed_params = array('Operation', 'ItemId', 'ResponseGroup', 'SearchIndex', 'Title');
	public $wanted_fields = array('author'=>'Author','isbn'=>'ISBN','pubdate'=>'PublicationDate','publisher'=>'Publisher','title'=>'Title','pages'=>'NumberOfPages');

	public function help(){
		echo '<pre> Sample usage:
$retriever = createHttpRequest("amazon");
$retriever->server = "ca";	//optional - default is .com
$retriever->public_key = "Your Access Key ID";
$retriever->private_key = "Your Secret Access Key";
$retriever->setParam("Title", "Le petit prince");
$result_array = $retriever->fetch_results("array");
$result_count = $retriever->get_result_count();

 Or by isbn/asin:
$retriever = createHttpRequest("amazon");
$retriever->public_key = "Your Access Key ID";
$retriever->private_key = "Your Secret Access Key";
$retriever->setParam("ItemId", 3265345645);
$result_array = $retriever->fetch_results("array");
</pre>';		
	}
	
	public function setQueryParam($value){
		$this->params['Title'] = $value;
	}
	
	public function standardize($array){
		$itemlist = array();
		foreach($array as $record){
			$newitem = array(	'title'=>$record['title'],
								'author'=>$record['author'],
								'date'=>$record['pubdate'],
								'year'=>$record['year'],
								'number'=>$record['isbn'],
								'link'=>false
							);
			array_push($itemlist, $newitem);
		}
		return $itemlist;
	}	

	public function fetch_results($return='array'){
		
		if(!$this->public_key || !$this->private_key){
			echo 'Api keys not given!';
			return false;
		}
		
		// inspired from a script of Jaap van Ganswijk (JvG)
		
		$params = $this->params;
		
		$parameters = array( 'ResponseGroup' => 'Medium', 'Operation' => 'ItemSearch' );	//Small, Medium, Large or SellerListing
		if( isset($params['ItemId']) && $params['ItemId'] != '' ){
			$parameters['Operation'] = 'ItemLookup';
		}elseif(isset($params['Title']) && $params['Title'] != ''){
			$parameters['SearchIndex'] = 'Books';
		}

		$parameters = array_merge($parameters, $params);
						  
		$url = $this->aws_signed_request($parameters);
	
		$content = $this->retrieve_page($url);	
		if(!$content)	return false;
				
		$xml = simplexml_load_string($content);
		if(!$xml)	return false;
		if(!$xml->Items->Item){
			$this->doDebug(true,'No item found.', 'Could not find $xml->Items->Item', __FILE__,__LINE__);
			return array();
		}
		
		$this->resultCount = $xml->Items->TotalResults;
		
		$fields = $this->wanted_fields;
		$itemlist = array();
		foreach ($xml->Items->Item as $item){
			$clean = array();
			foreach($fields as $local=>$remote)	$clean[$local] = isset($item->ItemAttributes->$remote)?$item->ItemAttributes->$remote:'';
			$clean['year'] = isset($item->ItemAttributes->PublicationDate)?substr($item->ItemAttributes->PublicationDate,0,4):'';
			if(isset($item->MediumImage->URL)){
				 $clean['image'] = '<img height="'.$item->MediumImage->Height.'" width="'.$item->MediumImage->Width.'" src="'.$item->MediumImage->URL.'" alt=""/>';
			}
			array_push($itemlist, $clean);
		}
		
		return $this->manage_return($itemlist, $return);
	
	}
	
	
	function aws_signed_request($params){	
		
	    /*
	    Copyright (c) 2009 Ulrich Mierendorff
	
	    Permission is hereby granted, free of charge, to any person obtaining a
	    copy of this software and associated documentation files (the "Software"),
	    to deal in the Software without restriction, including without limitation
	    the rights to use, copy, modify, merge, publish, distribute, sublicense,
	    and/or sell copies of the Software, and to permit persons to whom the
	    Software is furnished to do so, subject to the following conditions:
	
	    The above copyright notice and this permission notice shall be included in
	    all copies or substantial portions of the Software.
	
	    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
	    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
	    DEALINGS IN THE SOFTWARE.
	    */
	
	    // some paramters
	    $method = "GET";
	    $host = "ecs.amazonaws.".$this->server;
	    $uri = "/onca/xml";
	    
	    $public_key = $this->public_key;
	    $private_key = $this->private_key;
	    $region = $this->server;
	    
	    // additional parameters
	    $params["Service"] = "AWSECommerceService";
	    $params["AWSAccessKeyId"] = $public_key;
	    // GMT timestamp
	    $params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
	    // API version
	    $params["Version"] = "2009-03-31";
	    
	    // sort the parameters
	    ksort($params);
	    
	    // create the canonicalized query
	    $canonicalized_query = array();
	    foreach ($params as $param=>$value)
	    {
	        $param = str_replace("%7E", "~", rawurlencode($param));
	        $value = str_replace("%7E", "~", rawurlencode($value));
	        $canonicalized_query[] = $param."=".$value;
	    }
	    $canonicalized_query = implode("&", $canonicalized_query);
	    
	    // create the string to sign
	    $string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
	    
	    // calculate HMAC with SHA256 and base64-encoding
	    $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
	    
	    // encode the signature for the request
	    $signature = str_replace("%7E", "~", rawurlencode($signature));
	    
	    // create request
	    $request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
	
	    return $request;
	}

	
}

Return current item: Search results retriever