Amazon.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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-2008 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. * @category Zend
  24. * @package Zend_Service
  25. * @subpackage Amazon
  26. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Service_Amazon
  30. {
  31. /**
  32. * Amazon Web Services Access Key ID
  33. *
  34. * @var string
  35. */
  36. public $appId;
  37. /**
  38. * List of Amazon Web Service base URLs, indexed by country code
  39. *
  40. * @var array
  41. */
  42. protected $_baseUriList = array('US' => 'http://webservices.amazon.com',
  43. 'UK' => 'http://webservices.amazon.co.uk',
  44. 'DE' => 'http://webservices.amazon.de',
  45. 'JP' => 'http://webservices.amazon.co.jp',
  46. 'FR' => 'http://webservices.amazon.fr',
  47. 'CA' => 'http://webservices.amazon.ca');
  48. /**
  49. * Reference to REST client object
  50. *
  51. * @var Zend_Rest_Client
  52. */
  53. protected $_rest;
  54. /**
  55. * Constructs a new Amazon Web Services Client
  56. *
  57. * @param string $appId Developer's Amazon appid
  58. * @param string $countryCode Country code for Amazon service; may be US, UK, DE, JP, FR, CA
  59. * @throws Zend_Service_Exception
  60. * @return Zend_Service_Amazon
  61. */
  62. public function __construct($appId, $countryCode = 'US')
  63. {
  64. $this->appId = (string) $appId;
  65. $countryCode = (string) $countryCode;
  66. if (!isset($this->_baseUriList[$countryCode])) {
  67. /**
  68. * @see Zend_Service_Exception
  69. */
  70. require_once 'Zend/Service/Exception.php';
  71. throw new Zend_Service_Exception("Unknown country code: $countryCode");
  72. }
  73. /**
  74. * @see Zend_Rest_Client
  75. */
  76. require_once 'Zend/Rest/Client.php';
  77. $this->_rest = new Zend_Rest_Client($this->_baseUriList[$countryCode]);
  78. }
  79. /**
  80. * Search for Items
  81. *
  82. * @param array $options Options to use for the Search Query
  83. * @throws Zend_Service_Exception
  84. * @return Zend_Service_Amazon_ResultSet
  85. * @see http://www.amazon.com/gp/aws/sdk/main.html/102-9041115-9057709?s=AWSEcommerceService&v=2005-10-05&p=ApiReference/ItemSearchOperation
  86. */
  87. public function itemSearch(array $options)
  88. {
  89. $defaultOptions = array('ResponseGroup' => 'Small');
  90. $options = $this->_prepareOptions('ItemSearch', $options, $defaultOptions);
  91. $this->_rest->getHttpClient()->resetParameters();
  92. $response = $this->_rest->restGet('/onca/xml', $options);
  93. if ($response->isError()) {
  94. /**
  95. * @see Zend_Service_Exception
  96. */
  97. require_once 'Zend/Service/Exception.php';
  98. throw new Zend_Service_Exception('An error occurred sending request. Status code: '
  99. . $response->getStatus());
  100. }
  101. $dom = new DOMDocument();
  102. $dom->loadXML($response->getBody());
  103. self::_checkErrors($dom);
  104. /**
  105. * @see Zend_Service_Amazon_ResultSet
  106. */
  107. require_once 'Zend/Service/Amazon/ResultSet.php';
  108. return new Zend_Service_Amazon_ResultSet($dom);
  109. }
  110. /**
  111. * Look up item(s) by ASIN
  112. *
  113. * @param string $asin Amazon ASIN ID
  114. * @param array $options Query Options
  115. * @see http://www.amazon.com/gp/aws/sdk/main.html/102-9041115-9057709?s=AWSEcommerceService&v=2005-10-05&p=ApiReference/ItemLookupOperation
  116. * @throws Zend_Service_Exception
  117. * @return Zend_Service_Amazon_Item|Zend_Service_Amazon_ResultSet
  118. */
  119. public function itemLookup($asin, array $options = array())
  120. {
  121. $defaultOptions = array('IdType' => 'ASIN', 'ResponseGroup' => 'Small');
  122. $options['ItemId'] = (string) $asin;
  123. $options = $this->_prepareOptions('ItemLookup', $options, $defaultOptions);
  124. $this->_rest->getHttpClient()->resetParameters();
  125. $response = $this->_rest->restGet('/onca/xml', $options);
  126. if ($response->isError()) {
  127. /**
  128. * @see Zend_Service_Exception
  129. */
  130. require_once 'Zend/Service/Exception.php';
  131. throw new Zend_Service_Exception('An error occurred sending request. Status code: '
  132. . $response->getStatus());
  133. }
  134. $dom = new DOMDocument();
  135. $dom->loadXML($response->getBody());
  136. self::_checkErrors($dom);
  137. $xpath = new DOMXPath($dom);
  138. $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
  139. $items = $xpath->query('//az:Items/az:Item');
  140. if ($items->length == 1) {
  141. /**
  142. * @see Zend_Service_Amazon_Item
  143. */
  144. require_once 'Zend/Service/Amazon/Item.php';
  145. return new Zend_Service_Amazon_Item($items->item(0));
  146. }
  147. /**
  148. * @see Zend_Service_Amazon_ResultSet
  149. */
  150. require_once 'Zend/Service/Amazon/ResultSet.php';
  151. return new Zend_Service_Amazon_ResultSet($dom);
  152. }
  153. /**
  154. * Returns a reference to the REST client
  155. *
  156. * @return Zend_Rest_Client
  157. */
  158. public function getRestClient()
  159. {
  160. return $this->_rest;
  161. }
  162. /**
  163. * Prepare options for request
  164. *
  165. * @param string $query Action to perform
  166. * @param array $options User supplied options
  167. * @param array $defaultOptions Default options
  168. * @return array
  169. */
  170. protected function _prepareOptions($query, array $options, array $defaultOptions)
  171. {
  172. $options['SubscriptionId'] = $this->appId;
  173. $options['Service'] = 'AWSECommerceService';
  174. $options['Operation'] = (string) $query;
  175. // de-canonicalize out sort key
  176. if (isset($options['ResponseGroup'])) {
  177. $responseGroup = split(',', $options['ResponseGroup']);
  178. if (!in_array('Request', $responseGroup)) {
  179. $responseGroup[] = 'Request';
  180. $options['ResponseGroup'] = implode(',', $responseGroup);
  181. }
  182. }
  183. $options = array_merge($defaultOptions, $options);
  184. return $options;
  185. }
  186. /**
  187. * Check result for errors
  188. *
  189. * @param DOMDocument $dom
  190. * @throws Zend_Service_Exception
  191. * @return void
  192. */
  193. protected static function _checkErrors(DOMDocument $dom)
  194. {
  195. $xpath = new DOMXPath($dom);
  196. $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
  197. if ($xpath->query('//az:Error')->length >= 1) {
  198. $code = $xpath->query('//az:Error/az:Code/text()')->item(0)->data;
  199. $message = $xpath->query('//az:Error/az:Message/text()')->item(0)->data;
  200. switch($code) {
  201. case 'AWS.ECommerceService.NoExactMatches':
  202. break;
  203. default:
  204. /**
  205. * @see Zend_Service_Exception
  206. */
  207. require_once 'Zend/Service/Exception.php';
  208. throw new Zend_Service_Exception("$message ($code)");
  209. }
  210. }
  211. }
  212. }