Amazon.php 9.9 KB

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