|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00027 require_once 'Zend/Rest/Client.php'; 00028 00036 class Zend_Service_Amazon 00037 { 00043 public $appId; 00044 00048 protected $_secretKey = null; 00049 00053 protected $_baseUri = null; 00054 00060 protected $_baseUriList = array('US' => 'http://webservices.amazon.com', 00061 'UK' => 'http://webservices.amazon.co.uk', 00062 'DE' => 'http://webservices.amazon.de', 00063 'JP' => 'http://webservices.amazon.co.jp', 00064 'FR' => 'http://webservices.amazon.fr', 00065 'CA' => 'http://webservices.amazon.ca'); 00066 00072 protected $_rest = null; 00073 00074 00083 public function __construct($appId, $countryCode = 'US', $secretKey = null) 00084 { 00085 $this->appId = (string) $appId; 00086 $this->_secretKey = $secretKey; 00087 00088 $countryCode = (string) $countryCode; 00089 if (!isset($this->_baseUriList[$countryCode])) { 00093 require_once 'Zend/Service/Exception.php'; 00094 throw new Zend_Service_Exception("Unknown country code: $countryCode"); 00095 } 00096 00097 $this->_baseUri = $this->_baseUriList[$countryCode]; 00098 } 00099 00100 00109 public function itemSearch(array $options) 00110 { 00111 $client = $this->getRestClient(); 00112 $client->setUri($this->_baseUri); 00113 00114 $defaultOptions = array('ResponseGroup' => 'Small'); 00115 $options = $this->_prepareOptions('ItemSearch', $options, $defaultOptions); 00116 $client->getHttpClient()->resetParameters(); 00117 $response = $client->restGet('/onca/xml', $options); 00118 00119 if ($response->isError()) { 00123 require_once 'Zend/Service/Exception.php'; 00124 throw new Zend_Service_Exception('An error occurred sending request. Status code: ' 00125 . $response->getStatus()); 00126 } 00127 00128 $dom = new DOMDocument(); 00129 $dom->loadXML($response->getBody()); 00130 self::_checkErrors($dom); 00131 00135 require_once 'Zend/Service/Amazon/ResultSet.php'; 00136 return new Zend_Service_Amazon_ResultSet($dom); 00137 } 00138 00139 00149 public function itemLookup($asin, array $options = array()) 00150 { 00151 $client = $this->getRestClient(); 00152 $client->setUri($this->_baseUri); 00153 $client->getHttpClient()->resetParameters(); 00154 00155 $defaultOptions = array('ResponseGroup' => 'Small'); 00156 $options['ItemId'] = (string) $asin; 00157 $options = $this->_prepareOptions('ItemLookup', $options, $defaultOptions); 00158 $response = $client->restGet('/onca/xml', $options); 00159 00160 if ($response->isError()) { 00164 require_once 'Zend/Service/Exception.php'; 00165 throw new Zend_Service_Exception( 00166 'An error occurred sending request. Status code: ' . $response->getStatus() 00167 ); 00168 } 00169 00170 $dom = new DOMDocument(); 00171 $dom->loadXML($response->getBody()); 00172 self::_checkErrors($dom); 00173 $xpath = new DOMXPath($dom); 00174 $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05'); 00175 $items = $xpath->query('//az:Items/az:Item'); 00176 00177 if ($items->length == 1) { 00181 require_once 'Zend/Service/Amazon/Item.php'; 00182 return new Zend_Service_Amazon_Item($items->item(0)); 00183 } 00184 00188 require_once 'Zend/Service/Amazon/ResultSet.php'; 00189 return new Zend_Service_Amazon_ResultSet($dom); 00190 } 00191 00192 00198 public function getRestClient() 00199 { 00200 if($this->_rest === null) { 00201 $this->_rest = new Zend_Rest_Client(); 00202 } 00203 return $this->_rest; 00204 } 00205 00212 public function setRestClient(Zend_Rest_Client $client) 00213 { 00214 $this->_rest = $client; 00215 return $this; 00216 } 00217 00218 00227 protected function _prepareOptions($query, array $options, array $defaultOptions) 00228 { 00229 $options['AWSAccessKeyId'] = $this->appId; 00230 $options['Service'] = 'AWSECommerceService'; 00231 $options['Operation'] = (string) $query; 00232 $options['Version'] = '2005-10-05'; 00233 00234 // de-canonicalize out sort key 00235 if (isset($options['ResponseGroup'])) { 00236 $responseGroup = explode(',', $options['ResponseGroup']); 00237 00238 if (!in_array('Request', $responseGroup)) { 00239 $responseGroup[] = 'Request'; 00240 $options['ResponseGroup'] = implode(',', $responseGroup); 00241 } 00242 } 00243 00244 $options = array_merge($defaultOptions, $options); 00245 00246 if($this->_secretKey !== null) { 00247 $options['Timestamp'] = gmdate("Y-m-d\TH:i:s\Z");; 00248 ksort($options); 00249 $options['Signature'] = self::computeSignature($this->_baseUri, $this->_secretKey, $options); 00250 } 00251 00252 return $options; 00253 } 00254 00263 static public function computeSignature($baseUri, $secretKey, array $options) 00264 { 00265 require_once "Zend/Crypt/Hmac.php"; 00266 00267 $signature = self::buildRawSignature($baseUri, $options); 00268 return base64_encode( 00269 Zend_Crypt_Hmac::compute($secretKey, 'sha256', $signature, Zend_Crypt_Hmac::BINARY) 00270 ); 00271 } 00272 00280 static public function buildRawSignature($baseUri, $options) 00281 { 00282 ksort($options); 00283 $params = array(); 00284 foreach($options AS $k => $v) { 00285 $params[] = $k."=".rawurlencode($v); 00286 } 00287 00288 return sprintf("GET\n%s\n/onca/xml\n%s", 00289 str_replace('http://', '', $baseUri), 00290 implode("&", $params) 00291 ); 00292 } 00293 00294 00302 protected static function _checkErrors(DOMDocument $dom) 00303 { 00304 $xpath = new DOMXPath($dom); 00305 $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05'); 00306 00307 if ($xpath->query('//az:Error')->length >= 1) { 00308 $code = $xpath->query('//az:Error/az:Code/text()')->item(0)->data; 00309 $message = $xpath->query('//az:Error/az:Message/text()')->item(0)->data; 00310 00311 switch($code) { 00312 case 'AWS.ECommerceService.NoExactMatches': 00313 break; 00314 default: 00318 require_once 'Zend/Service/Exception.php'; 00319 throw new Zend_Service_Exception("$message ($code)"); 00320 } 00321 } 00322 } 00323 }