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: AmazonECS4.php,v 1.4 2006/08/04 10:31:00 ttsuruoka Exp $
  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. if (!defined('SERVICES_AMAZON_BASEURL')) {
  78.     define('SERVICES_AMAZON_BASEURL''http://webservices.amazon.com/onca/xml?Service=AWSECommerceService');
  79. }
  80. /**
  81. * A service version
  82. *
  83. * Use this to retrieve a particular version of the Amazon ECS.
  84. */
  85. if (!defined('SERVICES_AMAZON_ECSVERSION')) {
  86.     define('SERVICES_AMAZON_ECSVERSION''2005-10-05');
  87. }
  88.  
  89. /**
  90. * Class for accessing and retrieving information from Amazon's Web Services
  91. *
  92. @package Services_Amazon
  93. @author  John Downey <jdowney@gmail.com>
  94. @author  Tatsuya Tsuruoka <ttsuruoka@p4life.jp>
  95. @access  public
  96. @version Release: 0.7.1
  97. @uses    PEAR
  98. @uses    HTTP_Request
  99. @uses    XML_Unserializer
  100. */
  101. {
  102.     /**
  103.     * An Amazon AccessKey/Subscription ID used when quering Amazon servers
  104.     *
  105.     * @access private
  106.     * @var    string 
  107.     */
  108.     var $_keyid = null;
  109.  
  110.     /**
  111.     * An Amazon Associate ID used in the URL's so a commision may be payed
  112.     *
  113.     * @access private
  114.     * @var    string 
  115.     */
  116.     var $_associd = null;
  117.  
  118.     /**
  119.     * A base URL used to build the query for the Amazon servers
  120.     *
  121.     * @access private
  122.     * @var    string 
  123.     */
  124.     var $_baseurl = SERVICES_AMAZON_BASEURL;
  125.  
  126.     /**
  127.     * A service version
  128.     *
  129.     * @access private
  130.     * @var    string 
  131.     */
  132.     var $_version = SERVICES_AMAZON_ECSVERSION;
  133.  
  134.     /**
  135.     * The time that the Amazon took to process the request
  136.     * 
  137.     * @access private
  138.     * @var    string 
  139.     */
  140.     var $_processing_time = null;
  141.  
  142.     /**
  143.     * The last URL accessed to the Amazon (for debugging)
  144.     *
  145.     * @access private
  146.     * @var    string 
  147.     */
  148.     var $_lasturl = null;
  149.  
  150.     /**
  151.     * The raw result returned from the request
  152.     *
  153.     * @access private
  154.     * @var    string 
  155.     */
  156.     var $_raw_result = null;
  157.  
  158.     /**
  159.     * The cache object
  160.     *
  161.     * @access private
  162.     * @var    object 
  163.     */
  164.     var $_cache = null;
  165.  
  166.     /**
  167.     * The cache expire time
  168.     *
  169.     * Defaults to one hour.
  170.     *
  171.     * @access private
  172.     * @var    integer 
  173.     */
  174.     var $_cache_expire = 3600;
  175.  
  176.     /**
  177.     * Proxy server
  178.     *
  179.     * @access private
  180.     * @var    string 
  181.     */
  182.     var $_proxy_host = null;
  183.  
  184.     /**
  185.     * Proxy port
  186.     *
  187.     * @access private
  188.     * @var    integer 
  189.     */
  190.     var $_proxy_port = null;
  191.  
  192.     /**
  193.     * Proxy username
  194.     *
  195.     * @access private
  196.     * @var    string 
  197.     */
  198.     var $_proxy_user = null;
  199.  
  200.     /**
  201.     * Proxy password
  202.     *
  203.     * @access private
  204.     * @var    string 
  205.     */
  206.     var $_proxy_pass = null;
  207.  
  208.     /**
  209.     * Errors
  210.     *
  211.     * @access private
  212.     * @var    array 
  213.     */
  214.     var $_errors = array();
  215.  
  216.     /**
  217.     * Constructor
  218.     *
  219.     * @access public
  220.     * @param  string $keyid An Amazon Access Key ID used when quering Amazon servers
  221.     * @param  string $associd An Amazon Associate ID used in the URL's so a commision may be payed
  222.     * @see    setAccessKeyID
  223.     * @see    setAssociateID
  224.     * @see    setBaseUrl
  225.     * @see    setVersion
  226.     */
  227.     function Services_AmazonECS4($keyid$associd = null)
  228.     {
  229.         $this->_keyid $keyid;
  230.         $this->_associd $associd;
  231.     }
  232.  
  233.     /**
  234.     * Retrieves the current version of this classes API
  235.     *
  236.     * @access public
  237.     * @static
  238.     * @return string The API version
  239.     */
  240.     function getApiVersion()
  241.     {
  242.         return '0.7.1';
  243.     }
  244.  
  245.     /**
  246.     * Sets an Access Key ID
  247.     *
  248.     * @access public
  249.     * @param  string $subid An Access Key ID
  250.     * @return void 
  251.     */
  252.     function setAccessKeyID($keyid)
  253.     {
  254.         $this->_keyid $keyid;
  255.     }
  256.  
  257.     /**
  258.     * Sets a Subscription ID (for backward compatibility)
  259.     *
  260.     * @access public
  261.     * @param  string $subid A Subscription ID
  262.     * @return void 
  263.     */
  264.     function setSubscriptionID($subid)
  265.     {
  266.         $this->_keyid $subid;
  267.     }
  268.  
  269.     /**
  270.     * Sets an Associate ID
  271.     *
  272.     * @access public
  273.     * @param  string $associd An Associate ID
  274.     * @return void 
  275.     */
  276.     function setAssociateID($associd)
  277.     {
  278.         $this->_associd $associd;
  279.     }
  280.  
  281.     /**
  282.     * Sets the base URL
  283.     *
  284.     * @access public
  285.     * @param  string $url The base url
  286.     * @return void 
  287.     */
  288.     function setBaseUrl($url)
  289.     {
  290.         $this->_baseurl $url;
  291.     }
  292.  
  293.     /**
  294.     * Sets the locale passed when making a query to Amazon
  295.     *
  296.     * Currently US, UK, DE, JP, FR, and CA are supported
  297.     *
  298.     * @access public
  299.     * @param  string $locale The new locale to use
  300.     * @return mixed A PEAR_Error on error, a true on success
  301.     */
  302.     function setLocale($locale)
  303.     {
  304.         $urls = array(
  305.             'US' => 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService',
  306.             'UK' => 'http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService',
  307.             'DE' => 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
  308.             'JP' => 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService',
  309.             'FR' => 'http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService',
  310.             'CA' => 'http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService',
  311.         );
  312.         $locale strtoupper($locale);
  313.         if (empty($urls[$locale])) {
  314.             return PEAR::raiseError('Invalid locale');
  315.         }
  316.         $this->setBaseUrl($urls[$locale]);
  317.         return true;
  318.     }
  319.  
  320.     /**
  321.     * Sets a version
  322.     *
  323.     * @access public
  324.     * @param  string $version A service version
  325.     * @return void 
  326.     */
  327.     function setVersion($version)
  328.     {
  329.         $this->_version $version;
  330.     }
  331.  
  332.     /**
  333.     * Enables caching the data
  334.     *
  335.     * Requires Cache to be installed.
  336.     * Example:
  337.     * <code>
  338.     * <?php
  339.     * $amazon = new Services_AmazonECS4('[your Access Key ID here]');
  340.     * $amazon->setCache('file', array('cache_dir' => 'cache/'));
  341.     * $amazon->setCacheExpire(86400); // 86400 seconds = 24 hours
  342.     * $result = $amazon->BrowseNodeLookup('283155');
  343.     * ?>
  344.     * </code>
  345.     *
  346.     * @access public
  347.     * @param  string $container Name of container class
  348.     * @param  array $container_options Array with container class options
  349.     * @return mixed A PEAR_Error on error, a true on success
  350.     * @see    setCacheExpire()
  351.     */
  352.     function setCache($container 'file'$container_options = array())
  353.     {
  354.         if(!class_exists('Cache')){
  355.             @include_once 'Cache.php';
  356.         }
  357.         
  358.         @$cache = new Cache($container$container_options);
  359.         
  360.         if (is_object($cache)) {
  361.             $this->_cache $cache;
  362.         else {
  363.             $this->_cache = null;
  364.             return PEAR::raiseError('Cache init failed');
  365.         }
  366.  
  367.         return true;
  368.     }
  369.     
  370.     /**
  371.     * Sets cache expire time
  372.     * 
  373.     * Amazon dictates that any prices that are displayed that may be over an
  374.     * hour old should be accompanied by some sort of timestamp. You can get
  375.     * around that by expiring any queries that use the time in an hour (3600
  376.     * seconds).
  377.     *
  378.     * @access public
  379.     * @param  integer $secs Expire time in seconds
  380.     * @return void 
  381.     * @see    setCache()
  382.     */
  383.     function setCacheExpire($secs)
  384.     {
  385.         $this->_cache_expire $secs;
  386.     }
  387.  
  388.     /**
  389.     * Sets a proxy
  390.     *
  391.     * @access public
  392.     * @param string $host Proxy host
  393.     * @param int $port Proxy port
  394.     * @param string $user Proxy username
  395.     * @param string $pass Proxy password
  396.     */
  397.     function setProxy($host$port = 8080$user = null$pass = null)
  398.     {
  399.         $this->_proxy_host $host;
  400.         $this->_proxy_port $port;
  401.         $this->_proxy_user $user;
  402.         $this->_proxy_pass $pass;
  403.     }
  404.  
  405.     /**
  406.     * Retrieves all error codes and messages
  407.     *
  408.     * <code>
  409.     * if (PEAR::isError($result)) {
  410.     *     foreach ($amazon->getErrors() as $error) {
  411.     *         echo $error['Code'];
  412.     *         echo $error['Message'];
  413.     *     }
  414.     * }
  415.     * </code>
  416.     *
  417.     * @access public
  418.     * @return array All errors
  419.     */
  420.     function getErrors()
  421.     {
  422.         return $this->_errors;
  423.     }
  424.     
  425.     /**
  426.     * Retrieves the error code and message
  427.     *
  428.     * <code>
  429.     * if (PEAR::isError($result)) {
  430.     *     $error = $amazon->getError();
  431.     *     echo $error['Code'];
  432.     *     echo $error['Message'];
  433.     * }
  434.     * </code>
  435.     *
  436.     * @access public
  437.     * @return array All errors
  438.     */
  439.     function getError()
  440.     {
  441.         return current($this->_errors);
  442.     }
  443.  
  444.     /**
  445.     * Retrieves the processing time
  446.     *
  447.     * @access public
  448.     * @return string Processing time
  449.     */
  450.     function getProcessingTime()
  451.     {
  452.         return $this->_processing_time;
  453.     }
  454.  
  455.     /**
  456.     * Retrieves the last URL accessed to the Amazon (for debugging)
  457.     *
  458.     * @access public
  459.     * @return string The Last URL
  460.     */
  461.     function getLastUrl()
  462.     {
  463.         return $this->_lasturl;
  464.     }
  465.  
  466.     /**
  467.      * Retrieves the raw result
  468.      *
  469.      * @access public
  470.      * @return string The raw result
  471.      */
  472.     function getRawResult()
  473.     {
  474.         return $this->_raw_result;
  475.     }
  476.  
  477.     /**
  478.     * Retrieves information about a browse node
  479.     *
  480.     * Example:
  481.     * <code>
  482.     * <?php
  483.     * $amazon = new Services_AmazonECS4('[your Access Key ID here]');
  484.     * $result = $amazon->BrowseNodeLookup('283155'); // 283155='Books'
  485.     * ?>
  486.     * </code>
  487.     *
  488.     * @access public
  489.     * @param  string $browsenode_id The browse node ID
  490.     * @param  array $options The optional parameters
  491.     * @return array The array of information returned by the query
  492.     */
  493.     function BrowseNodeLookup($browsenode_id$options = array())
  494.     {
  495.         $params $options;
  496.         $params['Operation''BrowseNodeLookup';
  497.         $params['BrowseNodeId'$browsenode_id;
  498.         return $this->_sendRequest($params);
  499.     }
  500.  
  501.     /**
  502.     * Adds items to an existing remote shopping cart
  503.     *
  504.     * Example:
  505.     * <code>
  506.     * <?php
  507.     * $amazon = new Services_AmazonECS4('[your Access Key ID here]');
  508.     * $item = array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1);
  509.     * // $item = array(array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1),
  510.     * //               array('OfferListingId' => 'bbbbbbbbbb', 'Quantity' => 10),
  511.     * //               array('ASIN' => 'cccccccccc', 'Quantity' => 20));
  512.     * $result = $amazon->CartAdd('[Cart ID]', '[HMAC]', $item);
  513.     * ?>
  514.     * </code>
  515.     *
  516.     * @access public
  517.     * @param  string $cart_id A unique identifier for a cart
  518.     * @param  string $hmac A unique security token
  519.     * @param  array $item Products and the quantities
  520.     * @param  array $options The optional parameters
  521.     * @return array The array of information returned by the query
  522.     * @see    CartClear(), CartCreate(), CartModify()
  523.     */
  524.     function CartAdd($cart_id$hmac$item$options = array())
  525.     {
  526.         $params $options;
  527.         $params['Operation''CartAdd';
  528.         $params['CartId'$cart_id;
  529.         $params['HMAC'$hmac;
  530.         $params += $this->_assembleItemParameter($item);
  531.         return $this->_sendRequest($params);
  532.     }
  533.  
  534.     /**
  535.     * Removes all the contents of a remote shopping cart
  536.     *
  537.     * @access public
  538.     * @param  string $cart_id A unique identifier for a cart
  539.     * @param  string $hmac A unique security token
  540.     * @param  array $options The optional parameters
  541.     * @return array The array of information returned by the query
  542.     * @see    CartAdd(), CartCreate(), CartGet(), CartModify()
  543.     */
  544.     function CartClear($cart_id$hmac$options = array())
  545.     {
  546.         $params $options;
  547.         $params['Operation''CartClear';
  548.         $params['CartId'$cart_id;
  549.         $params['HMAC'$hmac;
  550.         return $this->_sendRequest($params);
  551.     }
  552.  
  553.     /**
  554.     * Creates a new remote shopping cart
  555.     *
  556.     * Example:
  557.     * <code>
  558.     * <?php
  559.     * $amazon = new Services_AmazonECS4('[your Access Key ID here]');
  560.     * $item = array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1);
  561.     * // $item = array(array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1),
  562.     * //               array('ASIN' => 'cccccccccc', 'Quantity' => 20));
  563.     * $result = $amazon->CartCreate($item);
  564.     * ?>
  565.     * </code>
  566.     *
  567.     * @access public
  568.     * @param  array $item Products and the quantities
  569.     * @param  array $options The optional parameters
  570.     * @return array The array of information returned by the query
  571.     * @see    CartAdd(), CartClear(), CartGet(), CartModify()
  572.     */
  573.     function CartCreate($item$options = array())
  574.     {
  575.         $params $options;
  576.         $params['Operation''CartCreate';
  577.         $params += $this->_assembleItemParameter($item);
  578.         return $this->_sendRequest($params);
  579.     }
  580.  
  581.     /**
  582.     * Retrieves the contents of a remote shopping cart
  583.     *
  584.     * @access public
  585.     * @param  string $cart_id A unique identifier for a cart
  586.     * @param  string $hmac A unique security token
  587.     * @param  array $options The optional parameters
  588.     * @return array The array of information returned by the query
  589.     * @see    CartAdd(), CartClear(), CartCreate(), CartModify()
  590.     */
  591.     function CartGet($cart_id$hmac$options = array())
  592.     {
  593.         $params $options;
  594.         $params['Operation''CartGet';
  595.         $params['CartId'$cart_id;
  596.         $params['HMAC'$hmac;
  597.         return $this->_sendRequest($params);
  598.     }
  599.  
  600.     /**
  601.     * Modifies the quantity of items in a cart and changes cart items to saved items
  602.     *
  603.     * Example:
  604.     * <code>
  605.     * <?php
  606.     * $amazon = new Services_AmazonECS4('[your Access Key ID here]');
  607.     * $item = array('CartItemId' => 'aaaaaaaaaa', 'Quantity' => 1);
  608.     * // $item = array('CartItemId' => 'aaaaaaaaaa', 'Action' => 'SaveForLater');
  609.     * // $item = array(array('CartItemId' => 'aaaaaaaaaa', 'Quantity' => 1),
  610.     * //               array('CartItemId' => 'cccccccccc', 'Quantity' => 20));
  611.     * $result = $amazon->CartModify('[Cart ID]', '[HMAC]', $item);
  612.     * ?>
  613.     * </code>
  614.     *
  615.     * @access public
  616.     * @param  string $cart_id A unique identifier for a cart
  617.     * @param  string $hmac A unique security token
  618.     * @param  array $item The CartItemId and the quantities or the Action
  619.     * @param  array $options The optional parameters
  620.     * @return array The array of information returned by the query
  621.     * @see    CartAdd(), CartClear(), CartCreate(), CartGet()
  622.     */
  623.     function CartModify($cart_id$hmac$item$options = array())
  624.     {
  625.         $params $options;
  626.         $params['Operation''CartModify';
  627.         $params['CartId'$cart_id;
  628.         $params['HMAC'$hmac;
  629.         $params += $this->_assembleItemParameter($item);
  630.         return $this->_sendRequest($params);
  631.     }
  632.  
  633.     /**
  634.     * Retrieves publicly available content written by specific Amazon customers
  635.     *
  636.     * @access public
  637.     * @param  string $customer_id A customer ID
  638.     * @param  array $options The optional parameters
  639.     * @return array The array of information returned by the query
  640.     * @see    CustomerContentSearch()
  641.     */
  642.     function CustomerContentLookup($customer_id$options = array())
  643.     {
  644.         $params $options;
  645.         $params['Operation''CustomerContentLookup';
  646.         $params['CustomerId'$customer_id;
  647.         return $this->_sendRequest($params);
  648.     }
  649.  
  650.     /**
  651.     * Searches for Amazon customers by name or email address
  652.     *
  653.     * @access public
  654.     * @param  array $customer A customer's name or its email
  655.     * @param  array $options The optional parameters
  656.     * @return array The array of information returned by the query
  657.     * @see    CustomerContentLookup()
  658.     */
  659.     function CustomerContentSearch($customer = null$options = array())
  660.     {
  661.         $params $options;
  662.         $params['Operation''CustomerContentSearch';
  663.         $params += $customer;
  664.         return $this->_sendRequest($params);
  665.     }
  666.  
  667.     /**
  668.     * Retrieves information about operations and response groups
  669.     *
  670.     * Example:
  671.     * <code>
  672.     * <?php
  673.     * $amazon = new Services_AmazonECS4('[your Access Key ID here]');
  674.     * $result = $amazon->Help('Operation', 'ItemLookup');
  675.     * ?>
  676.     * </code>
  677.     *
  678.     * @access public
  679.     * @param  string $help_type The type of information
  680.     * @param  string $about The name of an operation or a response group
  681.     * @param  array $options The optional parameters
  682.     * @return array The array of information returned by the query
  683.     */
  684.     function Help($help_type$about$options = array())
  685.     {
  686.         $params $options;
  687.         $params['Operation''Help';
  688.         $params['HelpType'$help_type;
  689.         $params['About'$about;
  690.         return $this->_sendRequest($params);
  691.     }
  692.         
  693.     /**
  694.     * Retrieves information for products
  695.     *
  696.     * Example:
  697.     * <code>
  698.     * <?php
  699.     * $amazon = new Services_AmazonECS4('[your Access Key ID here]');
  700.     * $options = array();
  701.     * $options['ResponseGroup'] = 'Large';
  702.     * $result = $amazon->ItemLookup('[ASIN(s)]', $options);
  703.     * ?>
  704.     * </code>
  705.     *
  706.     * @access public
  707.     * @param  string $item_id Product IDs
  708.     * @param  array $options The optional parameters
  709.     * @return array The array of information returned by the query
  710.     * @see    ItemSearch()
  711.     */
  712.     function ItemLookup($item_id$options = array())
  713.     {
  714.         $params $options;
  715.         $params['Operation''ItemLookup';
  716.         if (is_array($item_id)) {
  717.             $item_id implode(','$item_id);
  718.         }
  719.         $params['ItemId'$item_id;
  720.         return $this->_sendRequest($params);
  721.     }
  722.  
  723.     /**
  724.     * Searches for products
  725.     *
  726.     * Example:
  727.     * <code>
  728.     * <?php
  729.     * $amazon = new Services_AmazonECS4('[your Access Key ID here]');
  730.     * $options = array();
  731.     * $options['Keywords'] = 'sushi';
  732.     * $options['Sort'] = 'salesrank';
  733.     * $options['ResponseGroup'] = 'ItemIds,ItemAttributes,Images';
  734.     * $result = $amazon->ItemSearch('Books', $options);
  735.     * ?>
  736.     * </code>
  737.     *
  738.     * @access public
  739.     * @param  string $search_index A search index
  740.     * @param  array $options The optional parameters
  741.     * @return array The array of information returned by the query
  742.     * @see    ItemLookup()
  743.     */
  744.     function ItemSearch($search_index$options = array())
  745.     {
  746.         $params $options;
  747.         $params['Operation''ItemSearch';
  748.         $params['SearchIndex'$search_index;
  749.         return $this->_sendRequest($params);
  750.     }
  751.  
  752.     /**
  753.     * Retrieves products in a specific list
  754.     *
  755.     * @access public
  756.     * @param  string $list_type The type of list
  757.     * @param  string $list_id A list ID
  758.     * @param  array $options The optional parameters
  759.     * @return array The array of information returned by the query
  760.     * @see    ListSearch()
  761.     */
  762.     function ListLookup($list_type$list_id$options = array())
  763.     {
  764.         $params $options;
  765.         $params['Operation''ListLookup';
  766.         $params['ListType'$list_type;
  767.         $params['ListId'$list_id;
  768.         return $this->_sendRequest($params);
  769.     }
  770.  
  771.     /**
  772.     * Searches for a wish list, baby registry, or wedding registry
  773.     *
  774.     * Example:
  775.     * <code>
  776.     * <?php
  777.     * $amazon = new Services_AmazonECS4('[your Access Key ID here]');
  778.     * $keywords = array('Name' => 'hoge');
  779.     * $result = $amazon->ListSearch('WishList', $keywords);
  780.     * ?>
  781.     * </code>
  782.     *
  783.     * @access public
  784.     * @param  string $list_type The type of list
  785.     * @param  array $keywords Parameters to search for
  786.     * @param  array $options The optional parameters
  787.     * @return array The array of information returned by the query
  788.     * @see    ListLookup()
  789.     */
  790.     function ListSearch($list_type$keywords$options = array())
  791.     {
  792.         $params $options;
  793.         $params['Operation''ListSearch';
  794.         $params['ListType'$list_type;
  795.         $params += $keywords;
  796.         return $this->_sendRequest($params);
  797.     }
  798.  
  799.     /**
  800.     * Retrieves information about Amazon zShops and Marketplace products
  801.     *
  802.     * @access public
  803.     * @param  string $id_type The type of ID
  804.     * @param  string $id The exchange ID or the listing ID
  805.     * @param  array $options The optional parameters
  806.     * @return array The array of information returned by the query
  807.     * @see    SellerListingSearch()
  808.     */
  809.     function SellerListingLookup($id_type$id$options = array())
  810.     {
  811.         $params $options;
  812.         $params['Operation''SellerListingLookup';
  813.         $params['IdType'$id_type;
  814.         $params['Id'$id;
  815.         return $this->_sendRequest($params);
  816.     }
  817.  
  818.     /**
  819.     * Searches for Amazon zShops and Marketplace products
  820.     *
  821.     * Example:
  822.     * <code>
  823.     * <?php
  824.     * $amazon = new Services_AmazonECS4('[your Access Key ID here]');
  825.     * $keywords = array('Keywords' => 'pizza');
  826.     * $result = $amazon->SellerListingSearch('zShops', $keywords);
  827.     * ?>
  828.     * </code>
  829.     *
  830.     * @access public
  831.     * @param  string $search_index The type of seller listings
  832.     * @param  array $options The optional parameters
  833.     * @return array The array of information returned by the query
  834.     * @see    SellerListingLookup()
  835.     */
  836.     function SellerListingSearch($search_index$options = array())
  837.     {
  838.         $params $options;
  839.         $params['Operation''SellerListingSearch';
  840.         $params['SearchIndex'$search_index;
  841.         return $this->_sendRequest($params);
  842.     }
  843.  
  844.     /**
  845.     * Retrieves information about specific sellers
  846.     *
  847.     * @access public
  848.     * @param  string $seller_id IDs for Amazon sellers
  849.     * @param  array $options The optional parameters
  850.     * @return array The array of information returned by the query
  851.     */
  852.     function SellerLookup($seller_id$options = array())
  853.     {
  854.         $params $options;
  855.         $params['Operation''SellerLookup';
  856.         $params['SellerId'$seller_id;
  857.         return $this->_sendRequest($params);
  858.     }
  859.  
  860.     /**
  861.     * Retrieves products that are similar to Amazon products
  862.     *
  863.     * @access public
  864.     * @param  string $item_id Product IDs
  865.     * @param  array $options The optional parameters
  866.     * @return array The array of information returned by the query
  867.     */
  868.     function SimilarityLookup($item_id$options = array())
  869.     {
  870.         $params $options;
  871.         $params['Operation''SimilarityLookup';
  872.         if (is_array($item_id)) {
  873.             $item_id implode(','$item_id);
  874.         }
  875.         $params['ItemId'$item_id;
  876.         return $this->_sendRequest($params);
  877.     }
  878.  
  879.     /**
  880.     * Retrieves information about the status of financial transactions
  881.     *
  882.     * @access public
  883.     * @param  string $transaction_id Transaction IDs
  884.     * @param  array $options The optional parameters
  885.     * @return array The array of information returned by the query
  886.     */
  887.     function TransactionLookup($transaction_id$options = array())
  888.     {
  889.         $params $options;
  890.         $params['Operation''SimilarityLookup';
  891.         $params['TransactionId'$transaction_id;
  892.         return $this->_sendRequest($params);
  893.     }
  894.  
  895.     /**
  896.     * Combines requests for the same operation into a single request
  897.     *
  898.     * Example:
  899.     * <code>
  900.     * <?php
  901.     * $amazon = new Services_AmazonECS4('[your Access Key ID here]');
  902.     * $shared = array('SearchIndex' => 'Books',
  903.     *                 'Keywords' => 'php');
  904.     * $params1 = array('ItemPage' => '1');
  905.     * $params2 = array('ItemPage' => '2');
  906.     * $result = $amazon->doBatch('ItemSearch', $shared, $params1, $params2);
  907.     * ?>
  908.     * </code>
  909.     *
  910.     * @access public
  911.     * @param  string $operation The operation
  912.     * @param  array $shared Shared parameters
  913.     * @param  array $params1 The parameters specific to the first request
  914.     * @param  array $params2 The parameters specific to the second request
  915.     * @return array The array of information returned by the query
  916.     */
  917.     function doBatch($operation$shared$params1 = array()$params2 = array())
  918.     {
  919.         $params = array();
  920.         $params['Operation'$operation;
  921.         foreach ($shared as $k => $v{
  922.             $params[$operation '.Shared.' $k$v;
  923.         }
  924.         foreach ($params1 as $k => $v{
  925.             $params[$operation '.1.' $k$v;
  926.         }
  927.         foreach ($params2 as $k => $v{
  928.             $params[$operation '.2.' $k$v;
  929.         }
  930.         return $this->_sendRequest($params);
  931.     }
  932.  
  933.     /**
  934.     * Combines the different operations into a single request
  935.     *
  936.     * Example:
  937.     * <code>
  938.     * <?php
  939.     * $amazon = new Services_AmazonECS4('[your Access Key ID here]');
  940.     * $params1 = array('SearchIndex' => 'Books',
  941.     *                  'Title' => 'sushi');
  942.     * $params2 = array('Keywords' => 'tempura');
  943.     * $result = $amazon->doMultiOperation('ItemSearch', $params1,
  944.     *                                     'SellerListingSearch', $params2);
  945.     * ?>
  946.     * </code>
  947.     *
  948.     * @access public
  949.     * @param  string $operation1 The first operation
  950.     * @param  array $params1 The parameters specific to the first request
  951.     * @param  string $operation2 The second operation
  952.     * @param  array $params2 The parameters specific to the second request
  953.     * @return array The array of information returned by the query
  954.     */
  955.     function doMultiOperation($operation1$params1$operation2$params2)
  956.     {
  957.         $params = array();
  958.         $params['Operation'$operation1 ',' $operation2;
  959.         foreach ($params1 as $k => $v{
  960.             $params[$operation1 '.1.' $k$v;
  961.         }
  962.         foreach ($params2 as $k => $v{
  963.             $params[$operation2 '.1.' $k$v;
  964.         }
  965.         return $this->_sendRequest($params);
  966.     }
  967.  
  968.     /**
  969.     * Assembles the Item parameters
  970.     *
  971.     * @access private
  972.     * @param  array $items The items
  973.     * @return array The item parameters
  974.     */
  975.     function _assembleItemParameter($items)
  976.     {
  977.         $params = array();
  978.         if (!is_array(current($items))) {
  979.             $items = array(0 => $items);
  980.         }
  981.         $i = 1;
  982.         foreach ($items as $item{
  983.             foreach ($item as $k => $v{
  984.                 $params['Item.' $i '.' $k$v;
  985.             }
  986.             $i++;
  987.         }
  988.         return $params;
  989.     }
  990.  
  991.     /**
  992.     * Ignores the caching of specific operations
  993.     *
  994.     * @access private
  995.     * @param  string $operation The operation
  996.     * @return bool Returns true if the operation isn't cached, false otherwise
  997.     */
  998.     function _ignoreCache($operation)
  999.     {
  1000.         $ignore = array('CartAdd''CartClear''CartGet''CartModify''TransactionLookup');
  1001.         if (!strchr($operation',')) {
  1002.             return in_array($operation$ignore);
  1003.         }
  1004.         $operations explode(','$operation);
  1005.         foreach ($operations as $v{
  1006.             if (in_array($v$ignore)) {
  1007.                 return true;
  1008.             }
  1009.         }
  1010.         return false;
  1011.     }
  1012.  
  1013.     /**
  1014.     * Generates ID used as cache identifier
  1015.     *
  1016.     * @access private
  1017.     * @param  array $params 
  1018.     * @return string Cache ID
  1019.     */
  1020.     function _generateCacheId($params)
  1021.     {
  1022.         unset($params['AWSAccessKeyId']);
  1023.         unset($params['AssociateTag']);
  1024.         $str '';
  1025.         foreach ($params as $k => $v{
  1026.             $str .= $k $v;
  1027.         }
  1028.         return md5($str);
  1029.     }
  1030.  
  1031.     /**
  1032.     * Builds a URL
  1033.     *
  1034.     * @access private
  1035.     * @param array $params 
  1036.     * @return string URL
  1037.     */
  1038.     function _buildUrl($params)
  1039.     {
  1040.         $params['AWSAccessKeyId'$this->_keyid;
  1041.         $params['AssociateTag'$this->_associd;
  1042.         $params['Version'$this->_version;
  1043.         $url $this->_baseurl;
  1044.         foreach ($params as $k => $v{
  1045.             $url .= '&' $k '=' urlencode($v);
  1046.         }
  1047.         return $url;
  1048.     }
  1049.  
  1050.     /**
  1051.     * Sends a request
  1052.     *
  1053.     * @access private
  1054.     * @param string $url 
  1055.     * @return string The response
  1056.     */
  1057.     function _sendHttpRequest($url)
  1058.     {
  1059.         $http &new HTTP_Request($url);
  1060.         $http->setHttpVer('1.0');
  1061.         $http->addHeader('User-Agent''Services_AmazonECS4/' $this->getApiVersion());
  1062.         if ($this->_proxy_host{
  1063.             $http->setProxy($this->_proxy_host$this->_proxy_port$this->_proxy_user$this->_proxy_pass);
  1064.         }
  1065.  
  1066.         $result $http->sendRequest();
  1067.         if (PEAR::isError($result)) {
  1068.             return PEAR::raiseError('HTTP_Request::sendRequest failed: ' $result->message);
  1069.         }
  1070.  
  1071.         if ($http->getResponseCode(!= 200){
  1072.             return PEAR::raiseError('Amazon returned invalid HTTP response code ' $http->getResponseCode());
  1073.         }
  1074.         return $http->getResponseBody();
  1075.     }
  1076.  
  1077.     /**
  1078.     * Parses raw XML result
  1079.     *
  1080.     * @access private
  1081.     * @param string $raw_result 
  1082.     * @return string The contents
  1083.     */
  1084.     function _parseRawResult($raw_result)
  1085.     {
  1086.         $xml &new XML_Unserializer();
  1087.         $xml->setOption(XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSEtrue);
  1088.         $xml->setOption(XML_UNSERIALIZER_OPTION_FORCE_ENUM,
  1089.                         array('Item''Review''EditorialReview',
  1090.                               'Parameter''Author''Creator''ResponseGroup''Error'));
  1091.         $xml->unserialize($raw_resultfalse);
  1092.         $data $xml->getUnserializedData();
  1093.         if (PEAR::isError($data)) {
  1094.             return $data;
  1095.         }
  1096.  
  1097.         if (isset($data['Error'])) {
  1098.             $this->_errors $data['Error'];
  1099.             return PEAR::raiseError(implode(':'$this->getError()));
  1100.         }
  1101.  
  1102.         if (isset($data['OperationRequest']['RequestProcessingTime'])) {
  1103.             $this->_processing_time $data['OperationRequest']['RequestProcessingTime'];
  1104.         }
  1105.  
  1106.         if (isset($data['OperationRequest']['Errors'])) {
  1107.             $this->_errors $data['OperationRequest']['Errors']['Error'];
  1108.             return PEAR::raiseError(implode(':'$this->getError()));
  1109.         }
  1110.  
  1111.         // Get values of the second level content elements
  1112.         unset($data['xmlns']);
  1113.         unset($data['OperationRequest']);
  1114.         $contents = array();
  1115.         $keys array_keys($data);
  1116.         foreach ($keys as $v{
  1117.             if (strstr($v'Response')) {
  1118.                 $data[$vcurrent($data[$v]);
  1119.                 $contents[$v$data[$v];
  1120.             else {
  1121.                 $contents $data[$v];
  1122.             }
  1123.             $result $this->_checkContentError($data[$v]);
  1124.             if (PEAR::isError($result)) {
  1125.                 return $result;
  1126.             }
  1127.         }
  1128.         return $contents;
  1129.     }
  1130.  
  1131.     /**
  1132.     * Checks error codes at the content elements
  1133.     *
  1134.     * @access private
  1135.     * @param  array $content Values of the content elements
  1136.     * @return array mixed A PEAR_Error on error, a true on success
  1137.     * @see    _parseRawResult
  1138.     */
  1139.     function _checkContentError($content)
  1140.     {
  1141.         if (isset($content['Request']['Errors'])) {
  1142.             $this->_errors $content['Request']['Errors']['Error'];
  1143.             return PEAR::raiseError(implode(':'$this->getError()));
  1144.         else if (isset($content[0])) {
  1145.             $errors = array();
  1146.             foreach ($content as $v{
  1147.                 if (isset($v['Request']['Errors']['Error'])) {
  1148.                     $errors array_merge($errors$v['Request']['Errors']['Error']);
  1149.                 }
  1150.             }
  1151.             if (!empty($errors)) {
  1152.                 $this->_errors $errors;
  1153.                 return PEAR::raiseError(implode(':'$this->getError()));
  1154.             }
  1155.         }
  1156.         return true;
  1157.     }
  1158.  
  1159.     /**
  1160.     * Sends the request to Amazon
  1161.     *
  1162.     * @access private
  1163.     * @param  array $params The array of request parameters
  1164.     * @return array The array of information returned by the query
  1165.     */
  1166.     function _sendRequest($params)
  1167.     {
  1168.         $this->_errors = array();
  1169.  
  1170.         if (is_null($this->_keyid)) {
  1171.             return PEAR::raiseError('Access Key ID have not been set');
  1172.         }
  1173.  
  1174.         $url $this->_buildUrl($params);
  1175.         $this->_lasturl $url;
  1176.         if (PEAR::isError($url)) {
  1177.             return $url;
  1178.         }
  1179.  
  1180.         // Return cached data if available
  1181.         $cache_id = false;
  1182.         if (isset($this->_cache&& !$this->_ignoreCache($params['Operation'])) {
  1183.             $cache_id $this->_generateCacheId($params);
  1184.             $cache $this->_cache->get($cache_id);
  1185.             if (!is_null($cache)) {
  1186.                 $this->_processing_time = 0;
  1187.                 return $cache;
  1188.             }
  1189.         }
  1190.  
  1191.         $result $this->_sendHttpRequest($url);
  1192.         $this->_raw_result $result;
  1193.         if (PEAR::isError($result)) {
  1194.             return $result;
  1195.         }
  1196.  
  1197.         $contents $this->_parseRawResult($result);
  1198.         if (PEAR::isError($contents)) {
  1199.             return $contents;
  1200.         }
  1201.  
  1202.         if ($cache_id{
  1203.             $this->_cache->save($cache_id$contents$this->_cache_expire);
  1204.         }
  1205.  
  1206.         return $contents;
  1207.     }
  1208.  
  1209. }
  1210. ?>

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