PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/zend/Zend/Service/Amazon.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 323 lines | 142 code | 42 blank | 139 comment | 10 complexity | 417072118ace9893160ba60bd311a84b MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Amazon
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Rest_Client
  24. */
  25. require_once 'Zend/Rest/Client.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service
  29. * @subpackage Amazon
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Service_Amazon
  34. {
  35. /**
  36. * Amazon Web Services Access Key ID
  37. *
  38. * @var string
  39. */
  40. public $appId;
  41. /**
  42. * @var string
  43. */
  44. protected $_secretKey = null;
  45. /**
  46. * @var string
  47. */
  48. protected $_baseUri = null;
  49. /**
  50. * List of Amazon Web Service base URLs, indexed by country code
  51. *
  52. * @var array
  53. */
  54. protected $_baseUriList = array('US' => 'http://webservices.amazon.com',
  55. 'UK' => 'http://webservices.amazon.co.uk',
  56. 'DE' => 'http://webservices.amazon.de',
  57. 'JP' => 'http://webservices.amazon.co.jp',
  58. 'FR' => 'http://webservices.amazon.fr',
  59. 'CA' => 'http://webservices.amazon.ca');
  60. /**
  61. * Reference to REST client object
  62. *
  63. * @var Zend_Rest_Client
  64. */
  65. protected $_rest = null;
  66. /**
  67. * Constructs a new Amazon Web Services Client
  68. *
  69. * @param string $appId Developer's Amazon appid
  70. * @param string $countryCode Country code for Amazon service; may be US, UK, DE, JP, FR, CA
  71. * @throws Zend_Service_Exception
  72. * @return Zend_Service_Amazon
  73. */
  74. public function __construct($appId, $countryCode = 'US', $secretKey = null)
  75. {
  76. $this->appId = (string) $appId;
  77. $this->_secretKey = $secretKey;
  78. $countryCode = (string) $countryCode;
  79. if (!isset($this->_baseUriList[$countryCode])) {
  80. /**
  81. * @see Zend_Service_Exception
  82. */
  83. require_once 'Zend/Service/Exception.php';
  84. throw new Zend_Service_Exception("Unknown country code: $countryCode");
  85. }
  86. $this->_baseUri = $this->_baseUriList[$countryCode];
  87. }
  88. /**
  89. * Search for Items
  90. *
  91. * @param array $options Options to use for the Search Query
  92. * @throws Zend_Service_Exception
  93. * @return Zend_Service_Amazon_ResultSet
  94. * @see http://www.amazon.com/gp/aws/sdk/main.html/102-9041115-9057709?s=AWSEcommerceService&v=2005-10-05&p=ApiReference/ItemSearchOperation
  95. */
  96. public function itemSearch(array $options)
  97. {
  98. $client = $this->getRestClient();
  99. $client->setUri($this->_baseUri);
  100. $defaultOptions = array('ResponseGroup' => 'Small');
  101. $options = $this->_prepareOptions('ItemSearch', $options, $defaultOptions);
  102. $client->getHttpClient()->resetParameters();
  103. $response = $client->restGet('/onca/xml', $options);
  104. if ($response->isError()) {
  105. /**
  106. * @see Zend_Service_Exception
  107. */
  108. require_once 'Zend/Service/Exception.php';
  109. throw new Zend_Service_Exception('An error occurred sending request. Status code: '
  110. . $response->getStatus());
  111. }
  112. $dom = new DOMDocument();
  113. $dom->loadXML($response->getBody());
  114. self::_checkErrors($dom);
  115. /**
  116. * @see Zend_Service_Amazon_ResultSet
  117. */
  118. require_once 'Zend/Service/Amazon/ResultSet.php';
  119. return new Zend_Service_Amazon_ResultSet($dom);
  120. }
  121. /**
  122. * Look up item(s) by ASIN
  123. *
  124. * @param string $asin Amazon ASIN ID
  125. * @param array $options Query Options
  126. * @see http://www.amazon.com/gp/aws/sdk/main.html/102-9041115-9057709?s=AWSEcommerceService&v=2005-10-05&p=ApiReference/ItemLookupOperation
  127. * @throws Zend_Service_Exception
  128. * @return Zend_Service_Amazon_Item|Zend_Service_Amazon_ResultSet
  129. */
  130. public function itemLookup($asin, array $options = array())
  131. {
  132. $client = $this->getRestClient();
  133. $client->setUri($this->_baseUri);
  134. $client->getHttpClient()->resetParameters();
  135. $defaultOptions = array('ResponseGroup' => 'Small');
  136. $options['ItemId'] = (string) $asin;
  137. $options = $this->_prepareOptions('ItemLookup', $options, $defaultOptions);
  138. $response = $client->restGet('/onca/xml', $options);
  139. if ($response->isError()) {
  140. /**
  141. * @see Zend_Service_Exception
  142. */
  143. require_once 'Zend/Service/Exception.php';
  144. throw new Zend_Service_Exception(
  145. 'An error occurred sending request. Status code: ' . $response->getStatus()
  146. );
  147. }
  148. $dom = new DOMDocument();
  149. $dom->loadXML($response->getBody());
  150. self::_checkErrors($dom);
  151. $xpath = new DOMXPath($dom);
  152. $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
  153. $items = $xpath->query('//az:Items/az:Item');
  154. if ($items->length == 1) {
  155. /**
  156. * @see Zend_Service_Amazon_Item
  157. */
  158. require_once 'Zend/Service/Amazon/Item.php';
  159. return new Zend_Service_Amazon_Item($items->item(0));
  160. }
  161. /**
  162. * @see Zend_Service_Amazon_ResultSet
  163. */
  164. require_once 'Zend/Service/Amazon/ResultSet.php';
  165. return new Zend_Service_Amazon_ResultSet($dom);
  166. }
  167. /**
  168. * Returns a reference to the REST client
  169. *
  170. * @return Zend_Rest_Client
  171. */
  172. public function getRestClient()
  173. {
  174. if($this->_rest === null) {
  175. $this->_rest = new Zend_Rest_Client();
  176. }
  177. return $this->_rest;
  178. }
  179. /**
  180. * Set REST client
  181. *
  182. * @param Zend_Rest_Client
  183. * @return Zend_Service_Amazon
  184. */
  185. public function setRestClient(Zend_Rest_Client $client)
  186. {
  187. $this->_rest = $client;
  188. return $this;
  189. }
  190. /**
  191. * Prepare options for request
  192. *
  193. * @param string $query Action to perform
  194. * @param array $options User supplied options
  195. * @param array $defaultOptions Default options
  196. * @return array
  197. */
  198. protected function _prepareOptions($query, array $options, array $defaultOptions)
  199. {
  200. $options['AWSAccessKeyId'] = $this->appId;
  201. $options['Service'] = 'AWSECommerceService';
  202. $options['Operation'] = (string) $query;
  203. $options['Version'] = '2005-10-05';
  204. // de-canonicalize out sort key
  205. if (isset($options['ResponseGroup'])) {
  206. $responseGroup = explode(',', $options['ResponseGroup']);
  207. if (!in_array('Request', $responseGroup)) {
  208. $responseGroup[] = 'Request';
  209. $options['ResponseGroup'] = implode(',', $responseGroup);
  210. }
  211. }
  212. $options = array_merge($defaultOptions, $options);
  213. if($this->_secretKey !== null) {
  214. $options['Timestamp'] = gmdate("Y-m-d\TH:i:s\Z");;
  215. ksort($options);
  216. $options['Signature'] = self::computeSignature($this->_baseUri, $this->_secretKey, $options);
  217. }
  218. return $options;
  219. }
  220. /**
  221. * Compute Signature for Authentication with Amazon Product Advertising Webservices
  222. *
  223. * @param string $baseUri
  224. * @param string $secretKey
  225. * @param array $options
  226. * @return string
  227. */
  228. static public function computeSignature($baseUri, $secretKey, array $options)
  229. {
  230. require_once "Zend/Crypt/Hmac.php";
  231. $signature = self::buildRawSignature($baseUri, $options);
  232. return base64_encode(
  233. Zend_Crypt_Hmac::compute($secretKey, 'sha256', $signature, Zend_Crypt_Hmac::BINARY)
  234. );
  235. }
  236. /**
  237. * Build the Raw Signature Text
  238. *
  239. * @param string $baseUri
  240. * @param array $options
  241. * @return string
  242. */
  243. static public function buildRawSignature($baseUri, $options)
  244. {
  245. ksort($options);
  246. $params = array();
  247. foreach($options AS $k => $v) {
  248. $params[] = $k."=".rawurlencode($v);
  249. }
  250. return sprintf("GET\n%s\n/onca/xml\n%s",
  251. str_replace('http://', '', $baseUri),
  252. implode("&", $params)
  253. );
  254. }
  255. /**
  256. * Check result for errors
  257. *
  258. * @param DOMDocument $dom
  259. * @throws Zend_Service_Exception
  260. * @return void
  261. */
  262. protected static function _checkErrors(DOMDocument $dom)
  263. {
  264. $xpath = new DOMXPath($dom);
  265. $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
  266. if ($xpath->query('//az:Error')->length >= 1) {
  267. $code = $xpath->query('//az:Error/az:Code/text()')->item(0)->data;
  268. $message = $xpath->query('//az:Error/az:Message/text()')->item(0)->data;
  269. switch($code) {
  270. case 'AWS.ECommerceService.NoExactMatches':
  271. break;
  272. default:
  273. /**
  274. * @see Zend_Service_Exception
  275. */
  276. require_once 'Zend/Service/Exception.php';
  277. throw new Zend_Service_Exception("$message ($code)");
  278. }
  279. }
  280. }
  281. }