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

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