Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: tags/RELEASE_1…
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1007 lines (942 sloc) 31.312 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* Implementation of a developers backend for accessing Amazon's retail and
* assosciate services.
*
* PHP versions 4 and 5
*
* LICENSE: Copyright 2004 John Downey. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of The PEAR Group.
*
* @category Web Services
* @package Services_Amazon
* @author John Downey <jdowney@gmail.com>
* @author Tatsuya Tsuruoka <ttsuruoka@p4life.jp>
* @copyright 2004 John Downey
* @license http://www.freebsd.org/copyright/freebsd-license.html 2 Clause BSD License
* @version CVS: $Id$
* @link http://pear.php.net/package/Services_Amazon/
* @filesource
*/

/**
* Uses PEAR class for error management
*/
require_once 'PEAR.php';

/**
* Uses HTTP_Request class to send and receive data from Amazon web servers
*/
require_once 'HTTP/Request.php';

/**
* Uses XML_Unserializer class to parse data received from Amazon
*/
require_once 'XML/Unserializer.php';

/**
* A default base URL that is specific to the locale
*
* - Amazon.com (US)
* http://webservices.amazon.com/onca/xml?Service=AWSECommerceService
* - Amazon.co.uk (UK)
* http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService
* - Amazon.de (DE)
* http://webservices.amazon.de/onca/xml?Service=AWSECommerceService
* - Amazon.co.jp (JP)
* http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService
* - Amazon.fr (FR)
* http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService
* - Amazon.ca (CA)
* http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService
*/
define('SERVICES_AMAZON_BASEURL', 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService');

/**
* A service version
*
* Use this to retrieve a particular version of the Amazon ECS.
*/
define('SERVICES_AMAZON_ECSVERSION', '2005-07-26');


/**
* Class for accessing and retrieving information from Amazon's Web Services
*
* @package Services_Amazon
* @author John Downey <jdowney@gmail.com>
* @author Tatsuya Tsuruoka <ttsuruoka@p4life.jp>
* @access public
* @version Release: @package_version@
* @uses PEAR
* @uses HTTP_Request
* @uses XML_Unserializer
*/
class Services_AmazonECS4
{
    /**
* An Amazon Subscription ID used when quering Amazon servers
*
* @access private
* @var string
*/
    var $_subid = null;

    /**
* An Amazon Associate ID used in the URL's so a commision may be payed
*
* @access private
* @var string
*/
    var $_associd = null;

    /**
* A base URL used to build the query for the Amazon servers
*
* @access private
* @var string
*/
    var $_baseurl = SERVICES_AMAZON_BASEURL;

    /**
* A service version
*
* @access private
* @var string
*/
    var $_version = SERVICES_AMAZON_ECSVERSION;

    /**
* The time that the Amazon took to process the request
*
* @access private
* @var string
*/
    var $_processing_time = null;

    /**
* The last URL accessed to the Amazon (for debugging)
*
* @access private
* @var string
*/
    var $_lasturl = null;

    /**
* The cache object
*
* @access private
* @var object
*/
    var $_cache = null;

    /**
* The cache expire time
*
* Defaults to one hour.
*
* @access private
* @var integer
*/
    var $_cache_expire = 3600;

    /**
* Constructor
*
* @access public
* @param string $subid An Amazon Subscription ID used when quering Amazon servers
* @param string $associd An Amazon Associate ID used in the URL's so a commision may be payed
* @see setSubscriptionID
* @see setAssociateID
* @see setBaseUrl
* @see setVersion
*/
    function Services_AmazonECS4($subid, $associd = null)
    {
        $this->_subid = $subid;
        $this->_associd = $associd;
    }

    /**
* Retrieves the current version of this classes API
*
* @access public
* @static
* @return string The API version
*/
    function getApiVersion()
    {
        return '0.4';
    }

    /**
* Sets a Subscription ID
*
* @access public
* @param string $subid A Subscription ID
* @return void
*/
    function setSubscriptionID($subid)
    {
        $this->_subid = $subid;
    }

    /**
* Sets an Associate ID
*
* @access public
* @param string $associd An Associate ID
* @return void
*/
    function setAssociateID($associd)
    {
        $this->_associd = $associd;
    }

    /**
* Sets the base URL
*
* @access public
* @param string $url The base url
* @return void
*/
    function setBaseUrl($url)
    {
        $this->_baseurl = $url;
    }

    /**
* Sets the locale passed when making a query to Amazon
*
* Currently US, UK, DE, JP, FR, and CA are supported
*
* @access public
* @param string $locale The new locale to use
* @return mixed A PEAR_Error on error, a true on success
*/
    function setLocale($locale)
    {
        $urls = array('US' => 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService',
                      'UK' => 'http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService',
                      'DE' => 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
                      'JP' => 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService',
                      'FR' => 'http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService',
                      'CA' => 'http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService');
        $locale = strtoupper($locale);
        if (empty($urls[$locale])) {
            return PEAR::raiseError('Invalid locale');
        }
        $this->setBaseUrl($urls[$locale]);
        return true;
    }

    /**
* Sets a version
*
* @access public
* @param string $version A service version
* @return void
*/
    function setVersion($version)
    {
        $this->_version = $version;
    }

    /**
* Enables caching the data
*
* Requires Cache to be installed.
* Example:
* <code>
* <?php
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $amazon->setCache('file', array('cache_dir' => 'cache/'));
* $amazon->setCacheExpire(86400); // 86400 seconds = 24 hours
* $result = $amazon->BrowseNodeLookup('283155');
* ?>
* </code>
*
* @access public
* @param string $container Name of container class
* @param array $container_options Array with container class options
* @return mixed A PEAR_Error on error, a true on success
* @see setCacheExpire()
*/
    function setCache($container = 'file', $container_options = array())
    {
        if(!class_exists('Cache')){
            @include_once 'Cache.php';
        }
        
        @$cache = new Cache($container, $container_options);
        
        if (is_object($cache)) {
            $this->_cache = $cache;
        } else {
            $this->_cache = null;
            return PEAR::raiseError('Cache init failed');
        }

        return true;
    }
    
    /**
* Sets cache expire time
*
* Amazon dictates that any prices that are displayed that may be over an
* hour old should be accompanied by some sort of timestamp. You can get
* around that by expiring any queries that use the time in an hour (3600
* seconds).
*
* @access public
* @param integer $secs Expire time in seconds
* @return void
* @see setCache()
*/
    function setCacheExpire($secs)
    {
        $this->_cache_expire = $secs;
    }
    
    /**
* Retrieves the processing time
*
* @access public
* @return string Processing time
*/
    function getProcessingTime()
    {
        return $this->_processing_time;
    }

    /**
* Retrieves the last URL accessed to the Amazon (for debugging)
*
* @access public
* @return string The Last URL
*/
    function getLastUrl()
    {
        return $this->_lasturl;
    }

    /**
* Retrieves information about a browse node
*
* Example:
* <code>
* <?php
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $result = $amazon->BrowseNodeLookup('283155'); // 283155='Books'
* ?>
* </code>
*
* @access public
* @param string $browsenode_id The browse node ID
* @param array $options The optional parameters
* @return array The array of information returned by the query
*/
    function BrowseNodeLookup($browsenode_id, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'BrowseNodeLookup';
        $params['BrowseNodeId'] = $browsenode_id;
        return $this->_sendRequest($params);
    }

    /**
* Adds items to an existing remote shopping cart
*
* Example:
* <code>
* <?php
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $item = array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1);
* // $item = array(array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1),
* // array('OfferListingId' => 'bbbbbbbbbb', 'Quantity' => 10),
* // array('ASIN' => 'cccccccccc', 'Quantity' => 20));
* $result = $amazon->CartAdd('[Cart ID]', '[HMAC]', $item);
* ?>
* </code>
*
* @access public
* @param string $cart_id A unique identifier for a cart
* @param string $hmac A unique security token
* @param array $item Products and the quantities
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CartClear(), CartCreate(), CartModify()
*/
    function CartAdd($cart_id, $hmac, $item, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'CartAdd';
        $params['CartId'] = $cart_id;
        $params['HMAC'] = $hmac;
        $params += $this->_assembleItemParameter($item);
        return $this->_sendRequest($params);
    }

    /**
* Removes all the contents of a remote shopping cart
*
* @access public
* @param string $cart_id A unique identifier for a cart
* @param string $hmac A unique security token
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CartAdd(), CartCreate(), CartGet(), CartModify()
*/
    function CartClear($cart_id, $hmac, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'CartClear';
        $params['CartId'] = $cart_id;
        $params['HMAC'] = $hmac;
        return $this->_sendRequest($params);
    }

    /**
* Creates a new remote shopping cart
*
* Example:
* <code>
* <?php
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $item = array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1);
* // $item = array(array('ASIN' => 'aaaaaaaaaa', 'Quantity' => 1),
* // array('ASIN' => 'cccccccccc', 'Quantity' => 20));
* $result = $amazon->CartCreate($item);
* ?>
* </code>
*
* @access public
* @param array $item Products and the quantities
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CartAdd(), CartClear(), CartGet(), CartModify()
*/
    function CartCreate($item, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'CartCreate';
        $params += $this->_assembleItemParameter($item);
        return $this->_sendRequest($params);
    }

    /**
* Retrieves the contents of a remote shopping cart
*
* @access public
* @param string $cart_id A unique identifier for a cart
* @param string $hmac A unique security token
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CartAdd(), CartClear(), CartCreate(), CartModify()
*/
    function CartGet($cart_id, $hmac, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'CartGet';
        $params['CartId'] = $cart_id;
        $params['HMAC'] = $hmac;
        return $this->_sendRequest($params);
    }

    /**
* Modifies the quantity of items in a cart and changes cart items to saved items
*
* Example:
* <code>
* <?php
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $item = array('CartItemId' => 'aaaaaaaaaa', 'Quantity' => 1);
* // $item = array('CartItemId' => 'aaaaaaaaaa', 'Action' => 'SaveForLater');
* // $item = array(array('CartItemId' => 'aaaaaaaaaa', 'Quantity' => 1),
* // array('CartItemId' => 'cccccccccc', 'Quantity' => 20));
* $result = $amazon->CartModify('[Cart ID]', '[HMAC]', $item);
* ?>
* </code>
*
* @access public
* @param string $cart_id A unique identifier for a cart
* @param string $hmac A unique security token
* @param array $item The CartItemId and the quantities or the Action
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CartAdd(), CartClear(), CartCreate(), CartGet()
*/
    function CartModify($cart_id, $hmac, $item, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'CartModify';
        $params['CartId'] = $cart_id;
        $params['HMAC'] = $hmac;
        $params += $this->_assembleItemParameter($item);
        return $this->_sendRequest($params);
    }

    /**
* Retrieves publicly available content written by specific Amazon customers
*
* @access public
* @param string $customer_id A customer ID
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CustomerContentSearch()
*/
    function CustomerContentLookup($customer_id, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'CustomerContentLookup';
        $params['CustomerId'] = $customer_id;
        return $this->_sendRequest($params);
    }

    /**
* Searches for Amazon customers by name or email address
*
* @access public
* @param array $customer A customer's name or its email
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see CustomerContentLookup()
*/
    function CustomerContentSearch($customer = null, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'CustomerContentSearch';
        $params += $customer;
        return $this->_sendRequest($params);
    }

    /**
* Retrieves information about operations and response groups
*
* Example:
* <code>
* <?php
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $result = $amazon->Help('Operation', 'ItemLookup');
* ?>
* </code>
*
* @access public
* @param string $help_type The type of information
* @param string $about The name of an operation or a response group
* @param array $options The optional parameters
* @return array The array of information returned by the query
*/
    function Help($help_type, $about, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'Help';
        $params['HelpType'] = $help_type;
        $params['About'] = $about;
        return $this->_sendRequest($params);
    }
        
    /**
* Retrieves information for products
*
* Example:
* <code>
* <?php
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $options = array();
* $options['ResponseGroup'] = 'Large';
* $result = $amazon->ItemLookup('[ASIN(s)]', $options);
* ?>
* </code>
*
* @access public
* @param string $item_id Product IDs
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see ItemSearch()
*/
    function ItemLookup($item_id, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'ItemLookup';
        $params['ItemId'] = $item_id;
        return $this->_sendRequest($params);
    }

    /**
* Searches for products
*
* Example:
* <code>
* <?php
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $options = array();
* $options['Keywords'] = 'sushi';
* $options['Sort'] = 'salesrank';
* $options['ResponseGroup'] = 'ItemIds,ItemAttributes,Images';
* $result = $amazon->ItemSearch('Books', $options);
* ?>
* </code>
*
* @access public
* @param string $search_index A search index
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see ItemLookup()
*/
    function ItemSearch($search_index, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'ItemSearch';
        $params['SearchIndex'] = $search_index;
        return $this->_sendRequest($params);
    }

    /**
* Retrieves products in a specific list
*
* @access public
* @param string $list_type The type of list
* @param string $list_id A list ID
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see ListSearch()
*/
    function ListLookup($list_type, $list_id, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'ListLookup';
        $params['ListType'] = $list_type;
        $params['ListId'] = $list_id;
        return $this->_sendRequest($params);
    }

    /**
* Searches for a wish list, baby registry, or wedding registry
*
* Example:
* <code>
* <?php
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $keywords = array('Name' => 'hoge');
* $result = $amazon->ListSearch('WishList', $keywords);
* ?>
* </code>
*
* @access public
* @param string $list_type The type of list
* @param array $keywords Parameters to search for
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see ListLookup()
*/
    function ListSearch($list_type, $keywords, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'ListSearch';
        $params['ListType'] = $list_type;
        $params += $keywords;
        return $this->_sendRequest($params);
    }

    /**
* Retrieves information about Amazon zShops and Marketplace products
*
* @access public
* @param string $id_type The type of ID
* @param string $id The exchange ID or the listing ID
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see SellerListingSearch()
*/
    function SellerListingLookup($id_type, $id, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'SellerListingLookup';
        $params['IdType'] = $id_type;
        $params['Id'] = $id;
        return $this->_sendRequest($params);
    }

    /**
* Searches for Amazon zShops and Marketplace products
*
* Example:
* <code>
* <?php
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $keywords = array('Keywords' => 'pizza');
* $result = $amazon->SellerListingSearch('zShops', $keywords);
* ?>
* </code>
*
* @access public
* @param string $search_index The type of seller listings
* @param array $options The optional parameters
* @return array The array of information returned by the query
* @see SellerListingLookup()
*/
    function SellerListingSearch($search_index, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'SellerListingSearch';
        $params['SearchIndex'] = $search_index;
        return $this->_sendRequest($params);
    }

    /**
* Retrieves information about specific sellers
*
* @access public
* @param string $seller_id IDs for Amazon sellers
* @param array $options The optional parameters
* @return array The array of information returned by the query
*/
    function SellerLookup($seller_id, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'SellerLookup';
        $params['SellerId'] = $seller_id;
        return $this->_sendRequest($params);
    }

    /**
* Retrieves products that are similar to Amazon products
*
* @access public
* @param string $item_id Product IDs
* @param array $options The optional parameters
* @return array The array of information returned by the query
*/
    function SimilarityLookup($item_id, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'SimilarityLookup';
        $params['ItemId'] = $item_id;
        return $this->_sendRequest($params);
    }

    /**
* Retrieves information about the status of financial transactions
*
* @access public
* @param string $transaction_id Transaction IDs
* @param array $options The optional parameters
* @return array The array of information returned by the query
*/
    function TransactionLookup($transaction_id, $options = array())
    {
        $params = $options;
        $params['Operation'] = 'SimilarityLookup';
        $params['TransactionId'] = $transaction_id;
        return $this->_sendRequest($params);
    }

    /**
* Combines requests for the same operation into a single request
*
* Example:
* <code>
* <?php
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $shared = array('SearchIndex' => 'Books',
* 'Keywords' => 'php');
* $params1 = array('ItemPage' => '1');
* $params2 = array('ItemPage' => '2');
* $result = $amazon->doBatch('ItemSearch', $shared, $params1, $params2);
* ?>
* </code>
*
* @access public
* @param string $operation The operation
* @param array $shared Shared parameters
* @param array $params1 The parameters specific to the first request
* @param array $params2 The parameters specific to the second request
* @return array The array of information returned by the query
*/
    function doBatch($operation, $shared, $params1, $params2)
    {
        $params = array();
        $params['Operation'] = $operation;
        foreach ($shared as $k => $v) {
            $params[$operation . '.Shared.' . $k] = $v;
        }
        foreach ($params1 as $k => $v) {
            $params[$operation . '.1.' . $k] = $v;
        }
        foreach ($params2 as $k => $v) {
            $params[$operation . '.2.' . $k] = $v;
        }
        return $this->_sendRequest($params);
    }

    /**
* Combines the different operations into a single request
*
* Example:
* <code>
* <?php
* $amazon = new Services_AmazonECS4('[your Subscription ID here]');
* $params1 = array('SearchIndex' => 'Books',
* 'Title' => 'sushi');
* $params2 = array('Keywords' => 'tempura');
* $result = $amazon->doMultiOperation('ItemSearch', $params1,
* 'SellerListingSearch', $params2);
* ?>
* </code>
*
* @access public
* @param string $operation1 The first operation
* @param array $params1 The parameters specific to the first request
* @param string $operation2 The second operation
* @param array $params2 The parameters specific to the second request
* @return array The array of information returned by the query
*/
    function doMultiOperation($operation1, $params1, $operation2, $params2)
    {
        $params = array();
        $params['Operation'] = $operation1 . ',' . $operation2;
        foreach ($params1 as $k => $v) {
            $params[$operation1 . '.1.' . $k] = $v;
        }
        foreach ($params2 as $k => $v) {
            $params[$operation2 . '.1.' . $k] = $v;
        }
        return $this->_sendRequest($params);
    }

    /**
* Assembles the Item parameters
*
* @access private
* @param array $items The items
* @return array The item parameters
*/
    function _assembleItemParameter($items)
    {
        $params = array();
        if (!is_array(current($items))) {
            $items = array(0 => $items);
        }
        $i = 1;
        foreach ($items as $item) {
            foreach ($item as $k => $v) {
                $params['Item.' . $i . '.' . $k] = $v;
            }
            $i++;
        }
        return $params;
    }

    /**
* Ignores the caching of specific operations
*
* @access private
* @param string $operation The operation
* @return bool Returns true if the operation isn't cached, false otherwise
*/
    function _ignoreCache($operation)
    {
        $ignore = array('CartAdd', 'CartClear', 'CartGet', 'CartModify', 'TransactionLookup');
        if (!strchr($operation, ',')) {
            return in_array($operation, $ignore);
        }
        $operations = explode(',', $operation);
        foreach ($operations as $v) {
            if (in_array($v, $ignore)) {
                return true;
            }
        }
        return false;
    }

    /**
* Generates ID used as cache identifier
*
* @access private
* @param array $params
* @return string Cache ID
*/
    function _generateCacheId($params)
    {
        unset($params['SubscriptionId']);
        unset($params['AssociateTag']);
        $str = '';
        foreach ($params as $k => $v) {
            $str .= $k . $v;
        }
        return md5($str);
    }

    /**
* Sends the request to Amazons Web Services
*
* @access private
* @param array $params The array of request parameters
* @return array The array of information returned by the query
*/
    function _sendRequest($params)
    {
        if (is_null($this->_subid)) {
            return PEAR::raiseError('Subscription ID have not been set');
        }

        $params['SubscriptionId'] = $this->_subid;
        $params['AssociateTag'] = $this->_associd;
        $params['Version'] = $this->_version;
        $url = $this->_baseurl;
        foreach ($params as $k => $v) {
            $url .= '&' . $k . '=' . urlencode($v);
        }
        $this->_lasturl = $url;

        // Return cached data if available
        $cache_id = false;
        if (isset($this->_cache) && !$this->_ignoreCache($params['Operation'])) {
            $cache_id = $this->_generateCacheId($params);
            $cache = $this->_cache->get($cache_id);
            if (!is_null($cache)) {
                $this->_processing_time = 0;
                return $cache;
            }
        }

        $http = &new HTTP_Request($url);
        $http->addHeader('User-Agent', 'Services_AmazonECS4/' . $this->getApiVersion());
        $http->sendRequest();

        if ($http->getResponseCode() != 200){
            return PEAR::raiseError('Amazon returned invalid HTTP response code ' . $http->getResponseCode());
        }
        $result = $http->getResponseBody();

        $xml = &new XML_Unserializer();
        $xml->setOption(XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE, true);
        $xml->setOption(XML_UNSERIALIZER_OPTION_FORCE_ENUM, array('Item', 'Parameter', 'ResponseGroup'));
        $xml->unserialize($result, false);
        $data = $xml->getUnserializedData();

        if (isset($data['Error'])) {
            $errormsg = $data['Error']['Code'] . ': ' . $data['Error']['Message'];
            return PEAR::raiseError($errormsg);
        }

        if (isset($data['OperationRequest']['Errors'])) {
            $error = $data['OperationRequest']['Errors']['Error'];
            $errormsg = $error['Code'] . ': ' . $error['Message'];
            return PEAR::raiseError($errormsg);
        }
        
        $this->_processing_time = $data['OperationRequest']['RequestProcessingTime'];

        // Get values of the second level content elements
        unset($data['xmlns']);
        unset($data['OperationRequest']);
        $contents = array();
        $keys = array_keys($data);
        foreach ($keys as $v) {
            if (strstr($v, 'Response')) {
                $key = key($data[$v]);
                $data[$v] = $data[$v][$key];
                $contents[$v] = $data[$v];
            } else {
                $contents = $data[$v];
            }
            $result = $this->_checkContentError($data[$v]);
            if (PEAR::isError($result)) {
                return $result;
            }
        }

        if ($cache_id) {
            $this->_cache->save($cache_id, $contents, $this->_cache_expire);
        }

        return $contents;
    }

    /**
* Checks error codes at the content elements
*
* @access private
* @param array $content Values of the content elements
* @return array mixed A PEAR_Error on error, a true on success
*/
    function _checkContentError($content)
    {
        if (isset($content['Request']['Errors']['Error'])) {
            $error = $content['Request']['Errors']['Error'];
            if (isset($error['Code'])) {
                $errormsg = $error['Code'] . ':' . $error['Message'];
            } else {
                $errormsg = '';
                foreach ($error as $v) {
                    $errormsg .= $v['Code'] . ':' . $v['Message'] . "\n";
                }
            }
            return PEAR::raiseError($errormsg);
        }
        return true;
    }
}
?>
Something went wrong with that request. Please try again.