Source for file AmazonECS4.php
Documentation is available at AmazonECS4.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
* Implementation of a developers backend for accessing Amazon's retail and
* LICENSE: Copyright 2004 John Downey. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* o Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of The PEAR Group.
* @package Services_Amazon
* @author John Downey <jdowney@gmail.com>
* @author Tatsuya Tsuruoka <ttsuruoka@p4life.jp>
* @copyright 2004 John Downey
* @license http://www.freebsd.org/copyright/freebsd-license.html 2 Clause BSD License
* @link http://pear.php.net/package/Services_Amazon/
* Uses PEAR class for error management
* Uses HTTP_Request class to send and receive data from Amazon web servers
require_once 'HTTP/Request.php';
* Uses XML_Unserializer class to parse data received from Amazon
require_once 'XML/Unserializer.php';
* A default base URL that is specific to the locale
* http://webservices.amazon.com/onca/xml?Service=AWSECommerceService
* http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService
* http://webservices.amazon.de/onca/xml?Service=AWSECommerceService
* http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService
* http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService
* http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService
define('SERVICES_AMAZON_BASEURL', 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService');
* Use this to retrieve a particular version of the Amazon ECS.
define('SERVICES_AMAZON_ECSVERSION', '2005-07-26');
* Class for accessing and retrieving information from Amazon's Web Services
* @package Services_Amazon
* @author John Downey <jdowney@gmail.com>
* @author Tatsuya Tsuruoka <ttsuruoka@p4life.jp>
* @version Release: @package_version@
* An Amazon Subscription ID used when quering Amazon servers
* An Amazon Associate ID used in the URL's so a commision may be payed
* A base URL used to build the query for the Amazon servers
var $_baseurl = SERVICES_AMAZON_BASEURL;
var $_version = SERVICES_AMAZON_ECSVERSION;
* The time that the Amazon took to process the request
var $_processing_time = null;
* The last URL accessed to the Amazon (for debugging)
* @param string $subid An Amazon Subscription ID used when quering Amazon servers
* @param string $associd An Amazon Associate ID used in the URL's so a commision may be payed
$this->_associd = $associd;
* Retrieves the current version of this classes API
* @return string The API version
* @param string $subid A Subscription ID
* @param string $associd An Associate ID
$this->_associd = $associd;
* @param string $url The base url
* Sets the locale passed when making a query to Amazon
* Currently US, UK, DE, JP, FR, and CA are supported
* @param string $locale The new locale to use
* @return mixed A PEAR_Error on error, a true on success
$urls = array ('US' => 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService',
'UK' => 'http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService',
'DE' => 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
'JP' => 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService',
'FR' => 'http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService',
'CA' => 'http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService');
if (empty ($urls[$locale])) {
return PEAR ::raiseError ('Invalid locale');
* @param string $version A service version
$this->_version = $version;
* Retrieves the processing time
* @return string Processing time
return $this->_processing_time;
* Retrieves the last URL accessed to the Amazon (for debugging)
* @return string The Last URL
* Retrieves information about a browse node
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $result = $amazon->BrowseNodeLookup('283155'); // 283155='Books'
* @param string $browsenode_id The browse node ID
* @param array $options The optional parameters
* @return array The array of information returned by the query
$params['Operation'] = 'BrowseNodeLookup';
$params['BrowseNodeId'] = $browsenode_id;
return $this->_sendRequest ($params);
* Adds items to an existing remote shopping cart
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $item = array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1);
* // $item = array(array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1),
* // array('OfferListingId' => 'bbbbbbbbbb', 'Quantity' => 10),
* // array('ASIN' => 'cccccccccc', 'Quantity' => 20));
* $result = $amazon->CartAdd('[Cart ID]', '[HMAC]', $item);
* @param string $cart_id A unique identifier for a cart
* @param string $hmac A unique security token
* @param array $item Products and the quantities
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CartClear(), CartCreate(), CartModify()
function CartAdd($cart_id, $hmac, $item, $options = array ())
$params['Operation'] = 'CartAdd';
$params['CartId'] = $cart_id;
$params += $this->_assembleItemParameter ($item);
return $this->_sendRequest ($params);
* Removes all the contents of a remote shopping cart
* @param string $cart_id A unique identifier for a cart
* @param string $hmac A unique security token
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CartAdd(), CartCreate(), CartGet(), CartModify()
function CartClear($cart_id, $hmac, $options = array ())
$params['Operation'] = 'CartClear';
$params['CartId'] = $cart_id;
return $this->_sendRequest ($params);
* Creates a new remote shopping cart
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $item = array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1);
* // $item = array(array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1),
* // array('ASIN' => 'cccccccccc', 'Quantity' => 20));
* $result = $amazon->CartCreate($item);
* @param array $item Products and the quantities
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CartAdd(), CartClear(), CartGet(), CartModify()
$params['Operation'] = 'CartCreate';
$params += $this->_assembleItemParameter ($item);
return $this->_sendRequest ($params);
* Retrieves the contents of a remote shopping cart
* @param string $cart_id A unique identifier for a cart
* @param string $hmac A unique security token
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CartAdd(), CartClear(), CartCreate(), CartModify()
function CartGet($cart_id, $hmac, $options = array ())
$params['Operation'] = 'CartGet';
$params['CartId'] = $cart_id;
return $this->_sendRequest ($params);
* Modifies the quantity of items in a cart and changes cart items to saved items
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $item = array('CartItemId' => 'aaaaaaaaaa', 'Quantity' => 1);
* // $item = array('CartItemId' => 'aaaaaaaaaa', 'Action' => 'SaveForLater');
* // $item = array(array('CartItemId' => 'aaaaaaaaaa', 'Quantity' => 1),
* // array('CartItemId' => 'cccccccccc', 'Quantity' => 20));
* $result = $amazon->CartModify('[Cart ID]', '[HMAC]', $item);
* @param string $cart_id A unique identifier for a cart
* @param string $hmac A unique security token
* @param array $item The CartItemId and the quantities or the Action
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CartAdd(), CartClear(), CartCreate(), CartGet()
function CartModify($cart_id, $hmac, $item, $options = array ())
$params['Operation'] = 'CartModify';
$params['CartId'] = $cart_id;
$params += $this->_assembleItemParameter ($item);
return $this->_sendRequest ($params);
* Retrieves publicly available content written by specific Amazon customers
* @param string $customer_id A customer ID
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CustomerContentSearch()
$params['Operation'] = 'CustomerContentLookup';
$params['CustomerId'] = $customer_id;
return $this->_sendRequest ($params);
* Searches for Amazon customers by name or email address
* @param array $customer A customer's name or its email
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CustomerContentLookup()
$params['Operation'] = 'CustomerContentSearch';
return $this->_sendRequest ($params);
* Retrieves information about operations and response groups
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $result = $amazon->Help('Operation', 'ItemLookup');
* @param string $help_type The type of information
* @param string $about The name of an operation or a response group
* @param array $options The optional parameters
* @return array The array of information returned by the query
function Help($help_type, $about, $options = array ())
$params['Operation'] = 'Help';
$params['HelpType'] = $help_type;
$params['About'] = $about;
return $this->_sendRequest ($params);
* Retrieves information for products
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $options['ResponseGroup'] = 'Large';
* $result = $amazon->ItemLookup('[ASIN(s)]', $options);
* @param string $item_id Product IDs
* @param array $options The optional parameters
* @return array The array of information returned by the query
$params['Operation'] = 'ItemLookup';
$params['ItemId'] = $item_id;
return $this->_sendRequest ($params);
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $options['Keywords'] = 'sushi';
* $options['Sort'] = 'salesrank';
* $options['ResponseGroup'] = 'ItemIds,ItemAttributes,Images';
* $result = $amazon->ItemSearch('Books', $options);
* @param string $search_index A search index
* @param array $options The optional parameters
* @return array The array of information returned by the query
function ItemSearch($search_index, $options = array ())
$params['Operation'] = 'ItemSearch';
$params['SearchIndex'] = $search_index;
return $this->_sendRequest ($params);
* Retrieves products in a specific list
* @param string $list_type The type of list
* @param string $list_id A list ID
* @param array $options The optional parameters
* @return array The array of information returned by the query
function ListLookup($list_type, $list_id, $options = array ())
$params['Operation'] = 'ListLookup';
$params['ListType'] = $list_type;
$params['ListId'] = $list_id;
return $this->_sendRequest ($params);
* Searches for a wish list, baby registry, or wedding registry
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $keywords = array('Name' => 'hoge');
* $result = $amazon->ListSearch('WishList', $keywords);
* @param string $list_type The type of list
* @param array $keywords Parameters to search for
* @param array $options The optional parameters
* @return array The array of information returned by the query
function ListSearch($list_type, $keywords, $options = array ())
$params['Operation'] = 'ListSearch';
$params['ListType'] = $list_type;
return $this->_sendRequest ($params);
* Retrieves information about Amazon zShops and Marketplace products
* @param string $id_type The type of ID
* @param string $id The exchange ID or the listing ID
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see SellerListingSearch()
$params['Operation'] = 'SellerListingLookup';
$params['IdType'] = $id_type;
return $this->_sendRequest ($params);
* Searches for Amazon zShops and Marketplace products
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $keywords = array('Keywords' => 'pizza');
* $result = $amazon->SellerListingSearch('zShops', $keywords);
* @param string $search_index The type of seller listings
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see SellerListingLookup()
$params['Operation'] = 'SellerListingSearch';
$params['SearchIndex'] = $search_index;
return $this->_sendRequest ($params);
* Retrieves information about specific sellers
* @param string $seller_id IDs for Amazon sellers
* @param array $options The optional parameters
* @return array The array of information returned by the query
$params['Operation'] = 'SellerLookup';
$params['SellerId'] = $seller_id;
return $this->_sendRequest ($params);
* Retrieves products that are similar to Amazon products
* @param string $item_id Product IDs
* @param array $options The optional parameters
* @return array The array of information returned by the query
$params['Operation'] = 'SimilarityLookup';
$params['ItemId'] = $item_id;
return $this->_sendRequest ($params);
* Retrieves information about the status of financial transactions
* @param string $transaction_id Transaction IDs
* @param array $options The optional parameters
* @return array The array of information returned by the query
$params['Operation'] = 'SimilarityLookup';
$params['TransactionId'] = $transaction_id;
return $this->_sendRequest ($params);
* Combines requests for the same operation into a single request
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $shared = array('SearchIndex' => 'Books',
* $params1 = array('ItemPage' => '1');
* $params2 = array('ItemPage' => '2');
* $result = $amazon->doBatch('ItemSearch', $shared, $params1, $params2);
* @param string $operation The operation
* @param array $shared Shared parameters
* @param array $params1 The parameters specific to the first request
* @param array $params2 The parameters specific to the second request
* @return array The array of information returned by the query
function doBatch($operation, $shared, $params1, $params2)
$params['Operation'] = $operation;
foreach ($shared as $k => $v) {
$params[$operation . '.Shared.' . $k] = $v;
foreach ($params1 as $k => $v) {
$params[$operation . '.1.' . $k] = $v;
foreach ($params2 as $k => $v) {
$params[$operation . '.2.' . $k] = $v;
return $this->_sendRequest ($params);
* Combines the different operations into a single request
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $params1 = array('SearchIndex' => 'Books',
* $params2 = array('Keywords' => 'tempura');
* $result = $amazon->doMultiOperation('ItemSearch', $params1,
* 'SellerListingSearch', $params2);
* @param string $operation1 The first operation
* @param array $params1 The parameters specific to the first request
* @param string $operation2 The second operation
* @param array $params2 The parameters specific to the second request
* @return array The array of information returned by the query
$params['Operation'] = $operation1 . ',' . $operation2;
foreach ($params1 as $k => $v) {
$params[$operation1 . '.1.' . $k] = $v;
foreach ($params2 as $k => $v) {
$params[$operation2 . '.1.' . $k] = $v;
return $this->_sendRequest ($params);
* Assembles the Item parameters
* @param array $items The items
* @return array The item parameters
function _assembleItemParameter ($items)
$items = array (0 => $items);
foreach ($items as $item) {
foreach ($item as $k => $v) {
$params['Item.' . $i . '.' . $k] = $v;
* Sends the request to Amazons Web Services
* @param array $params The array of request parameters
* @return array The array of information returned by the query
function _sendRequest ($params)
return PEAR ::raiseError ('Subscription ID have not been set');
$url .= '&SubscriptionId=' . $this->_subid . '&AssociateTag=' . $this->_associd . '&Version=' . $this->_version;
foreach ($params as $k => $v) {
$http = &new HTTP_Request ($url);
$http->addHeader ('User-Agent', 'Services_AmazonECS4/' . $this->getApiVersion());
if($http->getResponseCode () != 200 ){
return PEAR ::raiseError ('Amazon returned invalid HTTP response code ' . $http->getResponseCode ());
$result = $http->getResponseBody ();
$xml = &new XML_Unserializer (array ('parseAttributes' => true ));
$xml->unserialize ($result, false );
$data = $xml->getUnserializedData ();
if (isset ($data['Error'])) {
$errormsg = $data['Error']['Code'] . ': ' . $data['Error']['Message'];
return PEAR ::raiseError ($errormsg);
$this->_processing_time = $data['OperationRequest']['RequestProcessingTime'];
// Gets values of the second level content elements
unset ($data['OperationRequest']);
$data[$v] = $data[$v][$key];
$contents[$v] = $data[$v];
$result = $this->_checkContentError ($data[$v]);
if (PEAR ::isError ($result)) {
* Checks error codes at the content elements
* @param array $content Values of the content elements
* @return array mixed A PEAR_Error on error, a true on success
function _checkContentError ($content)
$error = $content['Request']['Errors']['Error'];
if (isset ($error['Code'])) {
$errormsg = $error['Code'] . ':' . $error['Message'];
$errormsg .= $v['Code'] . ':' . $v['Message'] . "\n";
return PEAR ::raiseError ($errormsg);
Documentation generated on Mon, 11 Mar 2019 14:13:43 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|