Services_Amazon
[ class tree: Services_Amazon ] [ index: Services_Amazon ] [ all elements ]

Source for file AmazonECS4.php

Documentation is available at AmazonECS4.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4. * Implementation of a developers backend for accessing Amazon's retail and
  5. * assosciate services.
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * LICENSE: Copyright 2004 John Downey. All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * o Redistributions of source code must retain the above copyright notice, this
  15. *   list of conditions and the following disclaimer.
  16. * o Redistributions in binary form must reproduce the above copyright notice,
  17. *   this list of conditions and the following disclaimer in the documentation
  18. *   and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT "AS IS" AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  23. * EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  24. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  27. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  29. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * The views and conclusions contained in the software and documentation are
  32. * those of the authors and should not be interpreted as representing official
  33. * policies, either expressed or implied, of The PEAR Group.
  34. *
  35. @category  Web Services
  36. @package   Services_Amazon
  37. @author    John Downey <jdowney@gmail.com>
  38. @author    Tatsuya Tsuruoka <ttsuruoka@p4life.jp>
  39. @copyright 2004 John Downey
  40. @license   http://www.freebsd.org/copyright/freebsd-license.html 2 Clause BSD License
  41. @version   CVS: $Id:$
  42. @link      http://pear.php.net/package/Services_Amazon/
  43. @filesource
  44. */
  45.  
  46. /**
  47. * Uses PEAR class for error management
  48. */
  49. require_once 'PEAR.php';
  50.  
  51. /**
  52. * Uses HTTP_Request class to send and receive data from Amazon web servers
  53. */
  54. require_once 'HTTP/Request.php';
  55.  
  56. /**
  57. * Uses XML_Unserializer class to parse data received from Amazon
  58. */
  59. require_once 'XML/Unserializer.php';
  60.  
  61. /**
  62. * A default base URL that is specific to the locale
  63. *
  64. * - Amazon.com (US)
  65. *   http://webservices.amazon.com/onca/xml?Service=AWSECommerceService
  66. * - Amazon.co.uk (UK)
  67. *   http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService
  68. * - Amazon.de (DE)
  69. *   http://webservices.amazon.de/onca/xml?Service=AWSECommerceService
  70. * - Amazon.co.jp (JP)
  71. *   http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService
  72. * - Amazon.fr (FR)
  73. *   http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService
  74. * - Amazon.ca (CA)
  75. *   http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService
  76. */
  77. define('SERVICES_AMAZON_BASEURL''http://webservices.amazon.com/onca/xml?Service=AWSECommerceService');
  78.  
  79. /**
  80. * A service version
  81. *
  82. * Use this to retrieve a particular version of the Amazon ECS.
  83. */
  84. define('SERVICES_AMAZON_ECSVERSION''2005-07-26');
  85.  
  86.  
  87. /**
  88. * Class for accessing and retrieving information from Amazon's Web Services
  89. *
  90. @package Services_Amazon
  91. @author  John Downey <jdowney@gmail.com>
  92. @author  Tatsuya Tsuruoka <ttsuruoka@p4life.jp>
  93. @access  public
  94. @version Release: @package_version@
  95. @uses    PEAR
  96. @uses    HTTP_Request
  97. @uses    XML_Unserializer
  98. */
  99. {
  100.     /**
  101.     * An Amazon Subscription ID used when quering Amazon servers
  102.     *
  103.     * @access private
  104.     * @var    string 
  105.     */
  106.     var $_subid = null;
  107.  
  108.     /**
  109.     * An Amazon Associate ID used in the URL's so a commision may be payed
  110.     *
  111.     * @access private
  112.     * @var    string 
  113.     */
  114.     var $_associd = null;
  115.  
  116.     /**
  117.     * A base URL used to build the query for the Amazon servers
  118.     *
  119.     * @access private
  120.     * @var    string 
  121.     */
  122.     var $_baseurl = SERVICES_AMAZON_BASEURL;
  123.  
  124.     /**
  125.     * A service version
  126.     *
  127.     * @access private
  128.     * @var    string 
  129.     */
  130.     var $_version = SERVICES_AMAZON_ECSVERSION;
  131.  
  132.     /**
  133.     * The time that the Amazon took to process the request
  134.     * 
  135.     * @access private
  136.     * @var    string 
  137.     */
  138.     var $_processing_time = null;
  139.  
  140.     /**
  141.     * The last URL accessed to the Amazon (for debugging)
  142.     *
  143.     * @access private
  144.     * @var    string 
  145.     */
  146.     var $_lasturl = null;
  147.  
  148.     /**
  149.     * The cache object
  150.     *
  151.     * @access private
  152.     * @var    object 
  153.     */
  154.     var $_cache = null;
  155.  
  156.     /**
  157.     * The cache expire time
  158.     *
  159.     * Defaults to one hour.
  160.     *
  161.     * @access private
  162.     * @var    integer 
  163.     */
  164.     var $_cache_expire = 3600;
  165.  
  166.     /**
  167.     * Constructor
  168.     *
  169.     * @access public
  170.     * @param  string $subid An Amazon Subscription ID used when quering Amazon servers
  171.     * @param  string $associd An Amazon Associate ID used in the URL's so a commision may be payed
  172.     * @see    setSubscriptionID
  173.     * @see    setAssociateID
  174.     * @see    setBaseUrl
  175.     * @see    setVersion
  176.     */
  177.     function Services_AmazonECS4($subid$associd = null)
  178.     {
  179.         $this->_subid $subid;
  180.         $this->_associd $associd;
  181.     }
  182.  
  183.     /**
  184.     * Retrieves the current version of this classes API
  185.     *
  186.     * @access public
  187.     * @static
  188.     * @return string The API version
  189.     */
  190.     function getApiVersion()
  191.     {
  192.         return '0.4';
  193.     }
  194.  
  195.     /**
  196.     * Sets a Subscription ID
  197.     *
  198.     * @access public
  199.     * @param  string $subid A Subscription ID
  200.     * @return void 
  201.     */
  202.     function setSubscriptionID($subid)
  203.     {
  204.         $this->_subid $subid;
  205.     }
  206.  
  207.     /**
  208.     * Sets an Associate ID
  209.     *
  210.     * @access public
  211.     * @param  string $associd An Associate ID
  212.     * @return void 
  213.     */
  214.     function setAssociateID($associd)
  215.     {
  216.         $this->_associd $associd;
  217.     }
  218.  
  219.     /**
  220.     * Sets the base URL
  221.     *
  222.     * @access public
  223.     * @param  string $url The base url
  224.     * @return void 
  225.     */
  226.     function setBaseUrl($url)
  227.     {
  228.         $this->_baseurl $url;
  229.     }
  230.  
  231.     /**
  232.     * Sets the locale passed when making a query to Amazon
  233.     *
  234.     * Currently US, UK, DE, JP, FR, and CA are supported
  235.     *
  236.     * @access public
  237.     * @param  string $locale The new locale to use
  238.     * @return mixed A PEAR_Error on error, a true on success
  239.     */
  240.     function setLocale($locale)
  241.     {
  242.         $urls = array('US' => 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService',
  243.                       'UK' => 'http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService',
  244.                       'DE' => 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
  245.                       'JP' => 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService',
  246.                       'FR' => 'http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService',
  247.                       'CA' => 'http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService');
  248.         $locale strtoupper($locale);
  249.         if (empty($urls[$locale])) {
  250.             return PEAR::raiseError('Invalid locale');
  251.         }
  252.         $this->setBaseUrl($urls[$locale]);
  253.         return true;
  254.     }
  255.  
  256.     /**
  257.     * Sets a version
  258.     *
  259.     * @access public
  260.     * @param  string $version A service version
  261.     * @return void 
  262.     */
  263.     function setVersion($version)
  264.     {
  265.         $this->_version $version;
  266.     }
  267.  
  268.     /**
  269.     * Enables caching the data
  270.     *
  271.     * Requires Cache to be installed.
  272.     * Example:
  273.     * <code>
  274.     * <?php
  275.     * $amazon = new Services_AmazonECS4('[your Subscription ID here]');
  276.     * $amazon->setCache('file', array('cache_dir' => 'cache/'));
  277.     * $amazon->setCacheExpire(86400); // 86400 seconds = 24 hours
  278.     * $result = $amazon->BrowseNodeLookup('283155');
  279.     * ?>
  280.     * </code>
  281.     *
  282.     * @access public
  283.     * @param  string $container Name of container class
  284.     * @param  array $container_options Array with container class options
  285.     * @return mixed A PEAR_Error on error, a true on success
  286.     * @see    setCacheExpire()
  287.     */
  288.     function setCache($container 'file'$container_options = array())
  289.     {
  290.         if(!class_exists('Cache')){
  291.             @include_once 'Cache.php';
  292.         }
  293.         
  294.         @$cache = new Cache($container$container_options);
  295.         
  296.         if (is_object($cache)) {
  297.             $this->_cache $cache;
  298.         else {
  299.             $this->_cache = null;
  300.             return PEAR::raiseError('Cache init failed');
  301.         }
  302.  
  303.         return true;
  304.     }
  305.     
  306.     /**
  307.     * Sets cache expire time
  308.     * 
  309.     * Amazon dictates that any prices that are displayed that may be over an
  310.     * hour old should be accompanied by some sort of timestamp. You can get
  311.     * around that by expiring any queries that use the time in an hour (3600
  312.     * seconds).
  313.     *
  314.     * @access public
  315.     * @param  integer $secs Expire time in seconds
  316.     * @return void 
  317.     * @see    setCache()
  318.     */
  319.     function setCacheExpire($secs)
  320.     {
  321.         $this->_cache_expire $secs;
  322.     }
  323.     
  324.     /**
  325.     * Retrieves the processing time
  326.     *
  327.     * @access public
  328.     * @return string Processing time
  329.     */
  330.     function getProcessingTime()
  331.     {
  332.         return $this->_processing_time;
  333.     }
  334.  
  335.     /**
  336.     * Retrieves the last URL accessed to the Amazon (for debugging)
  337.     *
  338.     * @access public
  339.     * @return string The Last URL
  340.     */
  341.     function getLastUrl()
  342.     {
  343.         return $this->_lasturl;
  344.     }
  345.  
  346.     /**
  347.     * Retrieves information about a browse node
  348.     *
  349.     * Example:
  350.     * <code>
  351.     * <?php
  352.     * $amazon = new Services_AmazonECS4('[your Subscription ID here]');
  353.     * $result = $amazon->BrowseNodeLookup('283155'); // 283155='Books'
  354.     * ?>
  355.     * </code>
  356.     *
  357.     * @access public
  358.     * @param  string $browsenode_id The browse node ID
  359.     * @param  array $options The optional parameters
  360.     * @return array The array of information returned by the query
  361.     */
  362.     function BrowseNodeLookup($browsenode_id$options = array())
  363.     {
  364.         $params $options;
  365.         $params['Operation''BrowseNodeLookup';
  366.         $params['BrowseNodeId'$browsenode_id;
  367.         return $this->_sendRequest($params);
  368.     }
  369.  
  370.     /**
  371.     * Adds items to an existing remote shopping cart
  372.     *
  373.     * Example:
  374.     * <code>
  375.     * <?php
  376.     * $amazon = new Services_AmazonECS4('[your Subscription ID here]');
  377.     * $item = array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1);
  378.     * // $item = array(array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1),
  379.     * //               array('OfferListingId' => 'bbbbbbbbbb', 'Quantity' => 10),
  380.     * //               array('ASIN' => 'cccccccccc', 'Quantity' => 20));
  381.     * $result = $amazon->CartAdd('[Cart ID]', '[HMAC]', $item);
  382.     * ?>
  383.     * </code>
  384.     *
  385.     * @access public
  386.     * @param  string $cart_id A unique identifier for a cart
  387.     * @param  string $hmac A unique security token
  388.     * @param  array $item Products and the quantities
  389.     * @param  array $options The optional parameters
  390.     * @return array The array of information returned by the query
  391.     * @see    CartClear(), CartCreate(), CartModify()
  392.     */
  393.     function CartAdd($cart_id$hmac$item$options = array())
  394.     {
  395.         $params $options;
  396.         $params['Operation''CartAdd';
  397.         $params['CartId'$cart_id;
  398.         $params['HMAC'$hmac;
  399.         $params += $this->_assembleItemParameter($item);
  400.         return $this->_sendRequest($params);
  401.     }
  402.  
  403.     /**
  404.     * Removes all the contents of a remote shopping cart
  405.     *
  406.     * @access public
  407.     * @param  string $cart_id A unique identifier for a cart
  408.     * @param  string $hmac A unique security token
  409.     * @param  array $options The optional parameters
  410.     * @return array The array of information returned by the query
  411.     * @see    CartAdd(), CartCreate(), CartGet(), CartModify()
  412.     */
  413.     function CartClear($cart_id$hmac$options = array())
  414.     {
  415.         $params $options;
  416.         $params['Operation''CartClear';
  417.         $params['CartId'$cart_id;
  418.         $params['HMAC'$hmac;
  419.         return $this->_sendRequest($params);
  420.     }
  421.  
  422.     /**
  423.     * Creates a new remote shopping cart
  424.     *
  425.     * Example:
  426.     * <code>
  427.     * <?php
  428.     * $amazon = new Services_AmazonECS4('[your Subscription ID here]');
  429.     * $item = array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1);
  430.     * // $item = array(array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1),
  431.     * //               array('ASIN' => 'cccccccccc', 'Quantity' => 20));
  432.     * $result = $amazon->CartCreate($item);
  433.     * ?>
  434.     * </code>
  435.     *
  436.     * @access public
  437.     * @param  array $item Products and the quantities
  438.     * @param  array $options The optional parameters
  439.     * @return array The array of information returned by the query
  440.     * @see    CartAdd(), CartClear(), CartGet(), CartModify()
  441.     */
  442.     function CartCreate($item$options = array())
  443.     {
  444.         $params $options;
  445.         $params['Operation''CartCreate';
  446.         $params += $this->_assembleItemParameter($item);
  447.         return $this->_sendRequest($params);
  448.     }
  449.  
  450.     /**
  451.     * Retrieves the contents of a remote shopping cart
  452.     *
  453.     * @access public
  454.     * @param  string $cart_id A unique identifier for a cart
  455.     * @param  string $hmac A unique security token
  456.     * @param  array $options The optional parameters
  457.     * @return array The array of information returned by the query
  458.     * @see    CartAdd(), CartClear(), CartCreate(), CartModify()
  459.     */
  460.     function CartGet($cart_id$hmac$options = array())
  461.     {
  462.         $params $options;
  463.         $params['Operation''CartGet';
  464.         $params['CartId'$cart_id;
  465.         $params['HMAC'$hmac;
  466.         return $this->_sendRequest($params);
  467.     }
  468.  
  469.     /**
  470.     * Modifies the quantity of items in a cart and changes cart items to saved items
  471.     *
  472.     * Example:
  473.     * <code>
  474.     * <?php
  475.     * $amazon = new Services_AmazonECS4('[your Subscription ID here]');
  476.     * $item = array('CartItemId' => 'aaaaaaaaaa', 'Quantity' => 1);
  477.     * // $item = array('CartItemId' => 'aaaaaaaaaa', 'Action' => 'SaveForLater');
  478.     * // $item = array(array('CartItemId' => 'aaaaaaaaaa', 'Quantity' => 1),
  479.     * //               array('CartItemId' => 'cccccccccc', 'Quantity' => 20));
  480.     * $result = $amazon->CartModify('[Cart ID]', '[HMAC]', $item);
  481.     * ?>
  482.     * </code>
  483.     *
  484.     * @access public
  485.     * @param  string $cart_id A unique identifier for a cart
  486.     * @param  string $hmac A unique security token
  487.     * @param  array $item The CartItemId and the quantities or the Action
  488.     * @param  array $options The optional parameters
  489.     * @return array The array of information returned by the query
  490.     * @see    CartAdd(), CartClear(), CartCreate(), CartGet()
  491.     */
  492.     function CartModify($cart_id$hmac$item$options = array())
  493.     {
  494.         $params $options;
  495.         $params['Operation''CartModify';
  496.         $params['CartId'$cart_id;
  497.         $params['HMAC'$hmac;
  498.         $params += $this->_assembleItemParameter($item);
  499.         return $this->_sendRequest($params);
  500.     }
  501.  
  502.     /**
  503.     * Retrieves publicly available content written by specific Amazon customers
  504.     *
  505.     * @access public
  506.     * @param  string $customer_id A customer ID
  507.     * @param  array $options The optional parameters
  508.     * @return array The array of information returned by the query
  509.     * @see    CustomerContentSearch()
  510.     */
  511.     function CustomerContentLookup($customer_id$options = array())
  512.     {
  513.         $params $options;
  514.         $params['Operation''CustomerContentLookup';
  515.         $params['CustomerId'$customer_id;
  516.         return $this->_sendRequest($params);
  517.     }
  518.  
  519.     /**
  520.     * Searches for Amazon customers by name or email address
  521.     *
  522.     * @access public
  523.     * @param  array $customer A customer's name or its email
  524.     * @param  array $options The optional parameters
  525.     * @return array The array of information returned by the query
  526.     * @see    CustomerContentLookup()
  527.     */
  528.     function CustomerContentSearch($customer = null$options = array())
  529.     {
  530.         $params $options;
  531.         $params['Operation''CustomerContentSearch';
  532.         $params += $customer;
  533.         return $this->_sendRequest($params);
  534.     }
  535.  
  536.     /**
  537.     * Retrieves information about operations and response groups
  538.     *
  539.     * Example:
  540.     * <code>
  541.     * <?php
  542.     * $amazon = new Services_AmazonECS4('[your Subscription ID here]');
  543.     * $result = $amazon->Help('Operation', 'ItemLookup');
  544.     * ?>
  545.     * </code>
  546.     *
  547.     * @access public
  548.     * @param  string $help_type The type of information
  549.     * @param  string $about The name of an operation or a response group
  550.     * @param  array $options The optional parameters
  551.     * @return array The array of information returned by the query
  552.     */
  553.     function Help($help_type$about$options = array())
  554.     {
  555.         $params $options;
  556.         $params['Operation''Help';
  557.         $params['HelpType'$help_type;
  558.         $params['About'$about;
  559.         return $this->_sendRequest($params);
  560.     }
  561.         
  562.     /**
  563.     * Retrieves information for products
  564.     *
  565.     * Example:
  566.     * <code>
  567.     * <?php
  568.     * $amazon = new Services_AmazonECS4('[your Subscription ID here]');
  569.     * $options = array();
  570.     * $options['ResponseGroup'] = 'Large';
  571.     * $result = $amazon->ItemLookup('[ASIN(s)]', $options);
  572.     * ?>
  573.     * </code>
  574.     *
  575.     * @access public
  576.     * @param  string $item_id Product IDs
  577.     * @param  array $options The optional parameters
  578.     * @return array The array of information returned by the query
  579.     * @see    ItemSearch()
  580.     */
  581.     function ItemLookup($item_id$options = array())
  582.     {
  583.         $params $options;
  584.         $params['Operation''ItemLookup';
  585.         $params['ItemId'$item_id;
  586.         return $this->_sendRequest($params);
  587.     }
  588.  
  589.     /**
  590.     * Searches for products
  591.     *
  592.     * Example:
  593.     * <code>
  594.     * <?php
  595.     * $amazon = new Services_AmazonECS4('[your Subscription ID here]');
  596.     * $options = array();
  597.     * $options['Keywords'] = 'sushi';
  598.     * $options['Sort'] = 'salesrank';
  599.     * $options['ResponseGroup'] = 'ItemIds,ItemAttributes,Images';
  600.     * $result = $amazon->ItemSearch('Books', $options);
  601.     * ?>
  602.     * </code>
  603.     *
  604.     * @access public
  605.     * @param  string $search_index A search index
  606.     * @param  array $options The optional parameters
  607.     * @return array The array of information returned by the query
  608.     * @see    ItemLookup()
  609.     */
  610.     function ItemSearch($search_index$options = array())
  611.     {
  612.         $params $options;
  613.         $params['Operation''ItemSearch';
  614.         $params['SearchIndex'$search_index;
  615.         return $this->_sendRequest($params);
  616.     }
  617.  
  618.     /**
  619.     * Retrieves products in a specific list
  620.     *
  621.     * @access public
  622.     * @param  string $list_type The type of list
  623.     * @param  string $list_id A list ID
  624.     * @param  array $options The optional parameters
  625.     * @return array The array of information returned by the query
  626.     * @see    ListSearch()
  627.     */
  628.     function ListLookup($list_type$list_id$options = array())
  629.     {
  630.         $params $options;
  631.         $params['Operation''ListLookup';
  632.         $params['ListType'$list_type;
  633.         $params['ListId'$list_id;
  634.         return $this->_sendRequest($params);
  635.     }
  636.  
  637.     /**
  638.     * Searches for a wish list, baby registry, or wedding registry
  639.     *
  640.     * Example:
  641.     * <code>
  642.     * <?php
  643.     * $amazon = new Services_AmazonECS4('[your Subscription ID here]');
  644.     * $keywords = array('Name' => 'hoge');
  645.     * $result = $amazon->ListSearch('WishList', $keywords);
  646.     * ?>
  647.     * </code>
  648.     *
  649.     * @access public
  650.     * @param  string $list_type The type of list
  651.     * @param  array $keywords Parameters to search for
  652.     * @param  array $options The optional parameters
  653.     * @return array The array of information returned by the query
  654.     * @see    ListLookup()
  655.     */
  656.     function ListSearch($list_type$keywords$options = array())
  657.     {
  658.         $params $options;
  659.         $params['Operation''ListSearch';
  660.         $params['ListType'$list_type;
  661.         $params += $keywords;
  662.         return $this->_sendRequest($params);
  663.     }
  664.  
  665.     /**
  666.     * Retrieves information about Amazon zShops and Marketplace products
  667.     *
  668.     * @access public
  669.     * @param  string $id_type The type of ID
  670.     * @param  string $id The exchange ID or the listing ID
  671.     * @param  array $options The optional parameters
  672.     * @return array The array of information returned by the query
  673.     * @see    SellerListingSearch()
  674.     */
  675.     function SellerListingLookup($id_type$id$options = array())
  676.     {
  677.         $params $options;
  678.         $params['Operation''SellerListingLookup';
  679.         $params['IdType'$id_type;
  680.         $params['Id'$id;
  681.         return $this->_sendRequest($params);
  682.     }
  683.  
  684.     /**
  685.     * Searches for Amazon zShops and Marketplace products
  686.     *
  687.     * Example:
  688.     * <code>
  689.     * <?php
  690.     * $amazon = new Services_AmazonECS4('[your Subscription ID here]');
  691.     * $keywords = array('Keywords' => 'pizza');
  692.     * $result = $amazon->SellerListingSearch('zShops', $keywords);
  693.     * ?>
  694.     * </code>
  695.     *
  696.     * @access public
  697.     * @param  string $search_index The type of seller listings
  698.     * @param  array $options The optional parameters
  699.     * @return array The array of information returned by the query
  700.     * @see    SellerListingLookup()
  701.     */
  702.     function SellerListingSearch($search_index$options = array())
  703.     {
  704.         $params $options;
  705.         $params['Operation''SellerListingSearch';
  706.         $params['SearchIndex'$search_index;
  707.         return $this->_sendRequest($params);
  708.     }
  709.  
  710.     /**
  711.     * Retrieves information about specific sellers
  712.     *
  713.     * @access public
  714.     * @param  string $seller_id IDs for Amazon sellers
  715.     * @param  array $options The optional parameters
  716.     * @return array The array of information returned by the query
  717.     */
  718.     function SellerLookup($seller_id$options = array())
  719.     {
  720.         $params $options;
  721.         $params['Operation''SellerLookup';
  722.         $params['SellerId'$seller_id;
  723.         return $this->_sendRequest($params);
  724.     }
  725.  
  726.     /**
  727.     * Retrieves products that are similar to Amazon products
  728.     *
  729.     * @access public
  730.     * @param  string $item_id Product IDs
  731.     * @param  array $options The optional parameters
  732.     * @return array The array of information returned by the query
  733.     */
  734.     function SimilarityLookup($item_id$options = array())
  735.     {
  736.         $params $options;
  737.         $params['Operation''SimilarityLookup';
  738.         $params['ItemId'$item_id;
  739.         return $this->_sendRequest($params);
  740.     }
  741.  
  742.     /**
  743.     * Retrieves information about the status of financial transactions
  744.     *
  745.     * @access public
  746.     * @param  string $transaction_id Transaction IDs
  747.     * @param  array $options The optional parameters
  748.     * @return array The array of information returned by the query
  749.     */
  750.     function TransactionLookup($transaction_id$options = array())
  751.     {
  752.         $params $options;
  753.         $params['Operation''SimilarityLookup';
  754.         $params['TransactionId'$transaction_id;
  755.         return $this->_sendRequest($params);
  756.     }
  757.  
  758.     /**
  759.     * Combines requests for the same operation into a single request
  760.     *
  761.     * Example:
  762.     * <code>
  763.     * <?php
  764.     * $amazon = new Services_AmazonECS4('[your Subscription ID here]');
  765.     * $shared = array('SearchIndex' => 'Books',
  766.     *                 'Keywords' => 'php');
  767.     * $params1 = array('ItemPage' => '1');
  768.     * $params2 = array('ItemPage' => '2');
  769.     * $result = $amazon->doBatch('ItemSearch', $shared, $params1, $params2);
  770.     * ?>
  771.     * </code>
  772.     *
  773.     * @access public
  774.     * @param  string $operation The operation
  775.     * @param  array $shared Shared parameters
  776.     * @param  array $params1 The parameters specific to the first request
  777.     * @param  array $params2 The parameters specific to the second request
  778.     * @return array The array of information returned by the query
  779.     */
  780.     function doBatch($operation$shared$params1$params2)
  781.     {
  782.         $params = array();
  783.         $params['Operation'$operation;
  784.         foreach ($shared as $k => $v{
  785.             $params[$operation '.Shared.' $k$v;
  786.         }
  787.         foreach ($params1 as $k => $v{
  788.             $params[$operation '.1.' $k$v;
  789.         }
  790.         foreach ($params2 as $k => $v{
  791.             $params[$operation '.2.' $k$v;
  792.         }
  793.         return $this->_sendRequest($params);
  794.     }
  795.  
  796.     /**
  797.     * Combines the different operations into a single request
  798.     *
  799.     * Example:
  800.     * <code>
  801.     * <?php
  802.     * $amazon = new Services_AmazonECS4('[your Subscription ID here]');
  803.     * $params1 = array('SearchIndex' => 'Books',
  804.     *                  'Title' => 'sushi');
  805.     * $params2 = array('Keywords' => 'tempura');
  806.     * $result = $amazon->doMultiOperation('ItemSearch', $params1,
  807.     *                                     'SellerListingSearch', $params2);
  808.     * ?>
  809.     * </code>
  810.     *
  811.     * @access public
  812.     * @param  string $operation1 The first operation
  813.     * @param  array $params1 The parameters specific to the first request
  814.     * @param  string $operation2 The second operation
  815.     * @param  array $params2 The parameters specific to the second request
  816.     * @return array The array of information returned by the query
  817.     */
  818.     function doMultiOperation($operation1$params1$operation2$params2)
  819.     {
  820.         $params = array();
  821.         $params['Operation'$operation1 ',' $operation2;
  822.         foreach ($params1 as $k => $v{
  823.             $params[$operation1 '.1.' $k$v;
  824.         }
  825.         foreach ($params2 as $k => $v{
  826.             $params[$operation2 '.1.' $k$v;
  827.         }
  828.         return $this->_sendRequest($params);
  829.     }
  830.  
  831.     /**
  832.     * Assembles the Item parameters
  833.     *
  834.     * @access private
  835.     * @param  array $items The items
  836.     * @return array The item parameters
  837.     */
  838.     function _assembleItemParameter($items)
  839.     {
  840.         $params = array();
  841.         if (!is_array(current($items))) {
  842.             $items = array(0 => $items);
  843.         }
  844.         $i = 1;
  845.         foreach ($items as $item{
  846.             foreach ($item as $k => $v{
  847.                 $params['Item.' $i '.' $k$v;
  848.             }
  849.             $i++;
  850.         }
  851.         return $params;
  852.     }
  853.  
  854.     /**
  855.     * Ignores the caching of specific operations
  856.     *
  857.     * @access private
  858.     * @param  string $operation The operation
  859.     * @return bool Returns true if the operation isn't cached, false otherwise
  860.     */
  861.     function _ignoreCache($operation)
  862.     {
  863.         $ignore = array('CartAdd''CartClear''CartGet''CartModify''TransactionLookup');
  864.         if (!strchr($operation',')) {
  865.             return in_array($operation$ignore);
  866.         }
  867.         $operations explode(','$operation);
  868.         foreach ($operations as $v{
  869.             if (in_array($v$ignore)) {
  870.                 return true;
  871.             }
  872.         }
  873.         return false;
  874.     }
  875.  
  876.     /**
  877.     * Generates ID used as cache identifier
  878.     *
  879.     * @access private
  880.     * @param  array $params 
  881.     * @return string Cache ID
  882.     */
  883.     function _generateCacheId($params)
  884.     {
  885.         unset($params['SubscriptionId']);
  886.         unset($params['AssociateTag']);
  887.         $str '';
  888.         foreach ($params as $k => $v{
  889.             $str .= $k $v;
  890.         }
  891.         return md5($str);
  892.     }
  893.  
  894.     /**
  895.     * Sends the request to Amazons Web Services
  896.     *
  897.     * @access private
  898.     * @param  array $params The array of request parameters
  899.     * @return array The array of information returned by the query
  900.     */
  901.     function _sendRequest($params)
  902.     {
  903.         if (is_null($this->_subid)) {
  904.             return PEAR::raiseError('Subscription ID have not been set');
  905.         }
  906.  
  907.         $params['SubscriptionId'$this->_subid;
  908.         $params['AssociateTag'$this->_associd;
  909.         $params['Version'$this->_version;
  910.         $url $this->_baseurl;
  911.         foreach ($params as $k => $v{
  912.             $url .= '&' $k '=' urlencode($v);
  913.         }
  914.         $this->_lasturl $url;
  915.  
  916.         // Return cached data if available
  917.         $cache_id = false;
  918.         if (isset($this->_cache&& !$this->_ignoreCache($params['Operation'])) {
  919.             $cache_id $this->_generateCacheId($params);
  920.             $cache $this->_cache->get($cache_id);
  921.             if (!is_null($cache)) {
  922.                 $this->_processing_time = 0;
  923.                 return $cache;
  924.             }
  925.         }
  926.  
  927.         $http &new HTTP_Request($url);
  928.         $http->addHeader('User-Agent''Services_AmazonECS4/' $this->getApiVersion());
  929.         $http->sendRequest();
  930.  
  931.         if ($http->getResponseCode(!= 200){
  932.             return PEAR::raiseError('Amazon returned invalid HTTP response code ' $http->getResponseCode());
  933.         }
  934.         $result $http->getResponseBody();
  935.  
  936.         $xml &new XML_Unserializer(array('parseAttributes' => true));
  937.         $xml->unserialize($resultfalse);
  938.         $data $xml->getUnserializedData();
  939.  
  940.         if (isset($data['Error'])) {
  941.             $errormsg $data['Error']['Code'': ' $data['Error']['Message'];
  942.             return PEAR::raiseError($errormsg);
  943.         }
  944.  
  945.         if (isset($data['OperationRequest']['Errors'])) {
  946.             $error $data['OperationRequest']['Errors']['Error'];
  947.             $errormsg $error['Code'': ' $error['Message'];
  948.             return PEAR::raiseError($errormsg);
  949.         }
  950.         
  951.         $this->_processing_time $data['OperationRequest']['RequestProcessingTime'];
  952.  
  953.         // Get values of the second level content elements
  954.         unset($data['xmlns']);
  955.         unset($data['OperationRequest']);
  956.         $contents = array();
  957.         $keys array_keys($data);
  958.         foreach ($keys as $v{
  959.             if (strstr($v'Response')) {
  960.                 $key key($data[$v]);
  961.                 $data[$v$data[$v][$key];
  962.                 $contents[$v$data[$v];
  963.             else {
  964.                 $contents $data[$v];
  965.             }
  966.             $result $this->_checkContentError($data[$v]);
  967.             if (PEAR::isError($result)) {
  968.                 return $result;
  969.             }
  970.         }
  971.  
  972.         if ($cache_id{
  973.             $this->_cache->save($cache_id$contents$this->_cache_expire);
  974.         }
  975.  
  976.         return $contents;
  977.     }
  978.  
  979.     /**
  980.     * Checks error codes at the content elements
  981.     *
  982.     * @access private
  983.     * @param  array $content Values of the content elements
  984.     * @return array mixed A PEAR_Error on error, a true on success
  985.     */
  986.     function _checkContentError($content)
  987.     {
  988.         $error $content['Request']['Errors']['Error'];
  989.         if (isset($error)) {
  990.             if (isset($error['Code'])) {
  991.                 $errormsg $error['Code'':' $error['Message'];
  992.             else {
  993.                 $errormsg '';
  994.                 foreach ($error as $v{
  995.                     $errormsg .= $v['Code'':' $v['Message'"\n";
  996.                 }
  997.             }
  998.             return PEAR::raiseError($errormsg);
  999.         }
  1000.         return true;
  1001.     }
  1002. }
  1003. ?>

Documentation generated on Mon, 11 Mar 2019 14:12:17 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.