8
* This source file is subject to the new BSD license that is bundled
9
* with this package in the file LICENSE.txt.
10
* It is also available through the world-wide-web at this URL:
11
* http://framework.zend.com/license/new-bsd
12
* If you did not receive a copy of the license and are unable to
13
* obtain it through the world-wide-web, please send an email
14
* to license@zend.com so we can send you a copy immediately.
17
* @package Zend_Service
19
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
20
* @license http://framework.zend.com/license/new-bsd New BSD License
21
* @version $Id: Amazon.php 8064 2008-02-16 10:58:39Z thomas $
27
* @package Zend_Service
29
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
30
* @license http://framework.zend.com/license/new-bsd New BSD License
32
class Zend_Service_Amazon
35
* Amazon Web Services Access Key ID
42
* List of Amazon Web Service base URLs, indexed by country code
46
protected $_baseUriList = array('US' => 'http://webservices.amazon.com',
47
'UK' => 'http://webservices.amazon.co.uk',
48
'DE' => 'http://webservices.amazon.de',
49
'JP' => 'http://webservices.amazon.co.jp',
50
'FR' => 'http://webservices.amazon.fr',
51
'CA' => 'http://webservices.amazon.ca');
54
* Reference to REST client object
56
* @var Zend_Rest_Client
62
* Constructs a new Amazon Web Services Client
64
* @param string $appId Developer's Amazon appid
65
* @param string $countryCode Country code for Amazon service; may be US, UK, DE, JP, FR, CA
66
* @throws Zend_Service_Exception
67
* @return Zend_Service_Amazon
69
public function __construct($appId, $countryCode = 'US')
71
$this->appId = (string) $appId;
72
$countryCode = (string) $countryCode;
74
if (!isset($this->_baseUriList[$countryCode])) {
76
* @see Zend_Service_Exception
78
require_once 'Zend/Service/Exception.php';
79
throw new Zend_Service_Exception("Unknown country code: $countryCode");
83
* @see Zend_Rest_Client
85
require_once 'Zend/Rest/Client.php';
86
$this->_rest = new Zend_Rest_Client($this->_baseUriList[$countryCode]);
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=2005-10-05&p=ApiReference/ItemSearchOperation
98
public function itemSearch(array $options)
100
$defaultOptions = array('ResponseGroup' => 'Small');
101
$options = $this->_prepareOptions('ItemSearch', $options, $defaultOptions);
102
$this->_rest->getHttpClient()->resetParameters();
103
$response = $this->_rest->restGet('/onca/xml', $options);
105
if ($response->isError()) {
107
* @see Zend_Service_Exception
109
require_once 'Zend/Service/Exception.php';
110
throw new Zend_Service_Exception('An error occurred sending request. Status code: '
111
. $response->getStatus());
114
$dom = new DOMDocument();
115
$dom->loadXML($response->getBody());
116
self::_checkErrors($dom);
119
* @see Zend_Service_Amazon_ResultSet
121
require_once 'Zend/Service/Amazon/ResultSet.php';
122
return new Zend_Service_Amazon_ResultSet($dom);
127
* Look up item(s) by ASIN
129
* @param string $asin Amazon ASIN ID
130
* @param array $options Query Options
131
* @see http://www.amazon.com/gp/aws/sdk/main.html/102-9041115-9057709?s=AWSEcommerceService&v=2005-10-05&p=ApiReference/ItemLookupOperation
132
* @throws Zend_Service_Exception
133
* @return Zend_Service_Amazon_Item|Zend_Service_Amazon_ResultSet
135
public function itemLookup($asin, array $options = array())
137
$defaultOptions = array('IdType' => 'ASIN', 'ResponseGroup' => 'Small');
138
$options['ItemId'] = (string) $asin;
139
$options = $this->_prepareOptions('ItemLookup', $options, $defaultOptions);
140
$this->_rest->getHttpClient()->resetParameters();
141
$response = $this->_rest->restGet('/onca/xml', $options);
143
if ($response->isError()) {
145
* @see Zend_Service_Exception
147
require_once 'Zend/Service/Exception.php';
148
throw new Zend_Service_Exception('An error occurred sending request. Status code: '
149
. $response->getStatus());
152
$dom = new DOMDocument();
153
$dom->loadXML($response->getBody());
154
self::_checkErrors($dom);
155
$xpath = new DOMXPath($dom);
156
$xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
157
$items = $xpath->query('//az:Items/az:Item');
159
if ($items->length == 1) {
161
* @see Zend_Service_Amazon_Item
163
require_once 'Zend/Service/Amazon/Item.php';
164
return new Zend_Service_Amazon_Item($items->item(0));
168
* @see Zend_Service_Amazon_ResultSet
170
require_once 'Zend/Service/Amazon/ResultSet.php';
171
return new Zend_Service_Amazon_ResultSet($dom);
176
* Returns a reference to the REST client
178
* @return Zend_Rest_Client
180
public function getRestClient()
187
* Prepare options for request
189
* @param string $query Action to perform
190
* @param array $options User supplied options
191
* @param array $defaultOptions Default options
194
protected function _prepareOptions($query, array $options, array $defaultOptions)
196
$options['SubscriptionId'] = $this->appId;
197
$options['Service'] = 'AWSECommerceService';
198
$options['Operation'] = (string) $query;
200
// de-canonicalize out sort key
201
if (isset($options['ResponseGroup'])) {
202
$responseGroup = split(',', $options['ResponseGroup']);
204
if (!in_array('Request', $responseGroup)) {
205
$responseGroup[] = 'Request';
206
$options['ResponseGroup'] = implode(',', $responseGroup);
210
$options = array_merge($defaultOptions, $options);
216
* Check result for errors
218
* @param DOMDocument $dom
219
* @throws Zend_Service_Exception
222
protected static function _checkErrors(DOMDocument $dom)
224
$xpath = new DOMXPath($dom);
225
$xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
227
if ($xpath->query('//az:Error')->length >= 1) {
228
$code = $xpath->query('//az:Error/az:Code/text()')->item(0)->data;
229
$message = $xpath->query('//az:Error/az:Message/text()')->item(0)->data;
232
* @see Zend_Service_Exception
234
require_once 'Zend/Service/Exception.php';
235
throw new Zend_Service_Exception("$message ($code)");