home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Services / Amazon.php next >
Encoding:
PHP Script  |  2008-07-02  |  27.9 KB  |  820 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4. * Implementation of a developers backend for accessing Amazon.com'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: Amazon.php,v 1.2 2006/01/27 16:29:20 ttsuruoka Exp $
  42. * @link      http://pear.php.net/package/Services_Amazon/
  43. * @filesource
  44. */
  45.  
  46. // This class is for backward compatibility and should be considered obsolete.
  47. // You may as well use Services_AmazonECS4 when you create a new application.
  48.  
  49. /**
  50. * Uses PEAR class for error management.
  51. */
  52. require_once 'PEAR.php';
  53.  
  54. /**
  55. * Uses HTTP_Request class to send and receive data from Amazon web servers.
  56. */
  57. require_once 'HTTP/Request.php';
  58.  
  59. /**
  60. * Uses XML_Unserializer class to parse data received from Amazon.
  61. */
  62. require_once 'XML/Unserializer.php';
  63.  
  64. /**
  65. * Class for accessing and retrieving information from Amazon's Web Services.
  66. *
  67. * @package Services_Amazon
  68. * @author  John Downey <jdowney@gmail.com>
  69. * @author  Tatsuya Tsuruoka <ttsuruoka@p4life.jp>
  70. * @access  public
  71. * @version Release: 0.7.1
  72. * @uses    PEAR
  73. * @uses    HTTP_Request
  74. * @uses    XML_Unserializer
  75. */
  76. class Services_Amazon
  77. {
  78.     /**
  79.     * The developers token used when quering Amazon servers.
  80.     *
  81.     * @access private
  82.     * @var    string $_token
  83.     */
  84.     var $_token = null;
  85.     
  86.     /**
  87.     * An Amazon Associate ID used in the URL's so a commision may be payed.
  88.     *
  89.     * @access private
  90.     * @var    string $_affid
  91.     */
  92.     var $_affid = null;
  93.     
  94.     /**
  95.     * The locale to pass to Amazon.com's servers.
  96.     *
  97.     * @access private
  98.     * @var    string $_locale
  99.     */    
  100.     var $_locale = null;
  101.     
  102.     /**
  103.     * The base url used to build the query for the Amazon servers.
  104.     *
  105.     * @access private
  106.     * @var    string $_baseurl
  107.     */
  108.     var $_baseurl = null;
  109.  
  110.     /**
  111.     * Constructor
  112.     *
  113.     * @access public
  114.     * @param  string $token The developers token used when quering Amazon servers.
  115.     * @param  string $affid An Amazon Associate ID used in the URL's so a commision may be payed.
  116.     * @see    setToken
  117.     * @see    setAssociateID
  118.     * @see    setBaseUrl
  119.     * @see    setLocale
  120.     */
  121.     function Services_Amazon($token = null, $affid = null, $locale = 'us', $baseurl = 'http://webservices.amazon.com/onca/xml2') {
  122.         if (!is_null($token)) {
  123.             $this->_token = $token;
  124.         }
  125.  
  126.         if (!is_null($affid)) {
  127.             $this->_affid = $affid;
  128.         }
  129.         
  130.         $this->_baseurl = $baseurl;
  131.         $this->setLocale($locale);
  132.     }
  133.  
  134.     /**
  135.     * Retrieves the current version of this classes API.
  136.     *
  137.     * All major versions are backwards compatible with older version of the same
  138.     * version number. Such as 1.5 would work for a script written to use 1.0.
  139.     * However on the filp side a script that needs 1.5 would not work with
  140.     * API version 1.0.
  141.     *
  142.     * @access public
  143.     * @static
  144.     * @return string the API version
  145.     */
  146.     function getApiVersion() {
  147.         return '1.0';
  148.     }
  149.  
  150.     /**
  151.     * Retrieves the currently set Developer token.
  152.     * 
  153.     * To use Amazon's Web Services you need a developer's token. Visit
  154.     * {@link http://www.amazon.com/webservices} and read their license
  155.     * agreement to recieve a free token.
  156.     *
  157.     * @access public
  158.     * @return string the currently set Developer token
  159.     * @see    setToken()
  160.     */
  161.     function getToken() {
  162.         return $this->_token;
  163.     }
  164.  
  165.     /**
  166.     * Sets the Developer token to use when quering Amazon's Web Services.
  167.     *
  168.     * @access public
  169.     * @param  string $token your Developer token
  170.     * @return void
  171.     * @see    getToken()
  172.     */
  173.     function setToken($token) {
  174.         $this->_token = $token;
  175.     }
  176.  
  177.     /**
  178.     * Retrieves the currently set Associate ID.
  179.     *
  180.     * Your Associate ID is used to built the links to amazon which will give
  181.     * you credit for the sale. Visit {@link http://associates.amazon.com} to
  182.     * sign up for an Associate ID.
  183.     *
  184.     * @access public
  185.     * @return string the currently set Associate ID.
  186.     * @see    setAssociateID()
  187.     */
  188.     function getAssociateID() {
  189.         return $this->_affid;
  190.     }
  191.  
  192.     /**
  193.     * Sets the Associate ID to use when building links to Amazon.com.
  194.     *
  195.     * @access public
  196.     * @param  string $affid your Associate ID
  197.     * @return void
  198.     * @see    getAssociateID()
  199.     */
  200.     function setAssociateID($affid) {
  201.         $this->_affid = $affid;
  202.     }
  203.     
  204.     /**
  205.     * Retrieves the currently set base url.
  206.     *
  207.     * @access public
  208.     * @return string the currently set base url
  209.     * @see    setBaseUrl()
  210.     */
  211.     function getBaseUrl() {
  212.         return $this->_baseurl;
  213.     }
  214.  
  215.     /**
  216.     * Sets the base url used when making a query to Amazon.com.
  217.     *
  218.     * @access public
  219.     * @param  string $baseurl the base url to use
  220.     * @return void
  221.     * @see    getBaseUrl()
  222.     */
  223.     function setBaseUrl($baseurl) {
  224.         $this->_baseurl = $baseurl;
  225.     }
  226.     
  227.     /**
  228.     * Retrieves the locale passed when making a query to Amazon.com.
  229.     *
  230.     * @access public
  231.     * @return string the currently set locale
  232.     * @see    setLocale()
  233.     */    
  234.     function getLocale() {
  235.         return $this->_locale;
  236.     }
  237.     
  238.     /**
  239.     * Sets the locale passed when making a query to Amazon.
  240.     *
  241.     * Currently only us, uk, de, jp, fr and ca are supported by Amazon.
  242.     *
  243.     * @access public
  244.     * @param  string $locale the new locale to use
  245.     * @return void
  246.     * @see    getLocale()
  247.     */    
  248.     function setLocale($locale) {
  249.         $urls = array(
  250.             'us' => 'http://webservices.amazon.com/onca/xml2',
  251.             'uk' => 'http://webservices.amazon.co.uk/onca/xml2',
  252.             'de' => 'http://webservices.amazon.de/onca/xml2',
  253.             'jp' => 'http://webservices.amazon.co.jp/onca/xml2',
  254.             'fr' => 'http://webservices.amazon.fr/onca/xml2',
  255.             'ca' => 'http://webservices.amazon.ca/onca/xml2',
  256.         );
  257.         $this->_locale = strtolower($locale);
  258.         if (empty($urls[$locale])) {
  259.             return;
  260.         }
  261.         $this->setBaseUrl($urls[$locale]);
  262.     }
  263.     
  264.     /**
  265.     * Retrieves an array of modes supported by Amazon.
  266.     *
  267.     * The array is arranged with the shorthand version as the key and the human
  268.     * readable version as its value. Below are the current modes in the list.
  269.     * <pre>
  270.     * baby        - Baby
  271.     * books       - Books
  272.     * classical   - Classical Music
  273.     * dvd         - DVD
  274.     * electronics - Electronics
  275.     * garden      - Outdoor Living
  276.     * kitchen     - Kitchen & Housewares
  277.     * magazines   - Magazines
  278.     * music       - Popular Music
  279.     * pc-hardware - Computers
  280.     * photo       - Camera & Photo
  281.     * software    - Software
  282.     * toys        - Toys & Games
  283.     * universal   - Tools & Hardware
  284.     * vhs         - VHS
  285.     * videogames  - Computer & Video Games
  286.     * </pre>
  287.     *
  288.     * @access public
  289.     * @static
  290.     * @return array An array of modes with the short hand modename to pass to
  291.     *               Amazon as the key and the longer human readable form as the
  292.     *               key's value.
  293.     */
  294.     function getModes() {
  295.         return array('baby'        => 'Baby',
  296.                      'books'       => 'Books',
  297.                      'classical'   => 'Classical Music',
  298.                      'dvd'         => 'DVD',
  299.                      'electronics' => 'Electronics',
  300.                      'garden'      => 'Outdoor Living',
  301.                      'kitchen'     => 'Kitchen & Housewares',
  302.                      'magazines'   => 'Magazines',
  303.                      'music'       => 'Popular Music',
  304.                      'pc-hardware' => 'Computers',
  305.                      'photo'       => 'Camera & Photo',
  306.                      'software'    => 'Software',
  307.                      'toys'        => 'Toys & Games',
  308.                      'universal'   => 'Tools & Hardware',
  309.                      'vhs'         => 'VHS',
  310.                      'videogames'  => 'Computer & Video Games');
  311.     }
  312.  
  313.     /**
  314.     * Retrives the information of a product given its unique ASIN code.
  315.     *
  316.     * Amazon Standard Identification Numbers (ASINs) are unique blocks of 10
  317.     * letters and/or numbers that identify items. You can find the ASIN on the
  318.     * item's product information page at Amazon.com.
  319.     *
  320.     * Example:
  321.     * <code>
  322.     * <?php
  323.     * require_once 'PEAR.php';
  324.     * require_once 'Services/Amazon.php';
  325.     *
  326.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  327.     * $products = $amazon->searchAsin('0672325616');
  328.     *
  329.     * if(!PEAR::isError($products)) {
  330.     *     var_dump($products);
  331.     * } else {
  332.     *     echo $products->message;
  333.     * }
  334.     * ?>
  335.     * </code>
  336.     * If you were to fill in the Developer token in the constructor this would
  337.     * return the results for George Schlossnagle's book Advanced PHP
  338.     * Programming.
  339.     *
  340.     * @access public
  341.     * @param  string $asin The Amazon Standard Identification Number (ASIN)
  342.     *                      of the product your searching for
  343.     * @return array The array of products retrieved by the query.
  344.     */
  345.     function searchAsin($asin) {
  346.         return $this->_sendRequest(array('AsinSearch' => $asin), 1);
  347.     }
  348.  
  349.     /**
  350.     * Retrives the information of a book given its unique ISBN number.
  351.     *
  352.     * International Standard Book Numbers (ISBNs) are unique numbers that
  353.     * identify every book that is published. It is generally located below a
  354.     * barcode on the back of the book. Note: ISBN numbers are synonymous with
  355.     * ASIN numbers as Amazon uses the ISBN for a books ASIN.
  356.     *
  357.     * @access public
  358.     * @param  string $isbn The International Standard Book Number (ISBN) of the
  359.     *                      book you are searching for.
  360.     * @return array The array of products retrieved by the query.
  361.     * @see    searchAsin()
  362.     */
  363.     function searchIsbn($isbn) {
  364.         return $this->searchAsin($isbn, 1);
  365.     }
  366.     
  367.     /**
  368.     * Retrives the information of a product given its unique UPC number.
  369.     *
  370.     * Since Amazon usually carries the latest version of a product it may not
  371.     * be possible to find a product given its UPC even though a later version
  372.     * appears on Amazon.com.
  373.     *
  374.     * @access public
  375.     * @param  string $upc Universal Product Code (UPC) for the product you are
  376.     *                     searching for.
  377.     * @return array The array of products retrieved by the query.
  378.     */
  379.     function searchUpc($upc) {
  380.         return $this->_sendRequest(array('UpcSearch' => $upc), 1);
  381.     }
  382.     
  383.     /**
  384.     * Searches Amazon.com for a specific keyword.
  385.     *
  386.     * To limit your search to just a specific category such as books then set
  387.     * the $mode param to something other then null. See {@link getModes()} for a list of
  388.     * modes.
  389.     *
  390.     * Example:
  391.     * <code>
  392.     * <?php
  393.     * require_once 'PEAR.php';
  394.     * require_once 'Services/Amazon.php';
  395.     * 
  396.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  397.     * $products = $amazon->searchKeyword('PHP');
  398.     *
  399.     * if(!PEAR::isError($products)) {
  400.     *    var_dump($products);
  401.     * } else {
  402.     *    echo $products->message;
  403.     * }
  404.     * ?>
  405.     * </code>
  406.     * If you were to fill in the Developer token in the constructor this would
  407.     * search Amazon.com for all books about PHP and return the first 10 results.
  408.     * To retrieve more results you would pass a page number.
  409.     *
  410.     * @access public
  411.     * @param  string $keyword The keyword to search for.
  412.     * @param  string $mode The section of the site you wish to search in.
  413.     *                      Defaults to music.
  414.     * @param  interger $page Which page of products to retrieve. Defaults to
  415.     *                        the first.
  416.     * @return array The array of products retrieved by the query.
  417.     * @see    getModes()
  418.     */
  419.     function searchKeyword($keyword, $mode = null, $page = 1) {
  420.         return $this->_sendRequest(array('KeywordSearch' => $keyword, 'mode' => $mode), $page);
  421.     }
  422.  
  423.     /**
  424.     * Searches Amazon for products similer to the Asin passed to it.
  425.     *
  426.     * To limit your search to just a specific category such as books then set
  427.     * the $mode param to something other then null. See {@link getModes()} for a list of
  428.     * modes. If the $mode param is null it will search all modes.
  429.     *
  430.     * Example:
  431.     * <code>
  432.     * <?php
  433.     * require_once 'PEAR.php';
  434.     * require_once 'Services/Amazon.php';
  435.     * 
  436.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  437.     * $products = $amazon->searchSimilar('0672325616', 'books');
  438.     *
  439.     * if(!PEAR::isError($products)) {
  440.     *    var_dump($products);
  441.     * } else {
  442.     *    echo $products->message;
  443.     * }
  444.     * ?>
  445.     * </code>
  446.     * If you were to fill in the Developer token in the constructor this would
  447.     * search Amazon.com for all books related to George Schlossnagle's book
  448.     * Advanced PHP Programming. To retrieve more results you would pass a page
  449.     * number.
  450.     *
  451.     * @access public
  452.     * @param  string $asin The Asin of the product that is similer to what you
  453.     *                      are searching for.
  454.     * @param  string $mode The section of the site you wish to search in 
  455.     * @param  interger $page Which page of products to retrieve. Defaults to
  456.     *                        the first.
  457.     * @return array The array of products retrieved by the query.
  458.     * @see    getModes()
  459.     */
  460.     function searchSimilar($asin, $mode = null, $page = 1) {
  461.         return $this->_sendRequest(array('SimilaritySearch' => $asin, 'mode' => $mode), $page);
  462.     }
  463.  
  464.     /**
  465.     * Searches Amazon.com for a specific author.
  466.     *
  467.     * Example:
  468.     * <code>
  469.     * <?php
  470.     * require_once 'PEAR.php';
  471.     * require_once 'Services/Amazon.php';
  472.     * 
  473.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  474.     * $products = $amazon->searchAuthor('Frank Herbert');
  475.     *
  476.     * if(!PEAR::isError($products)) {
  477.     *    var_dump($products);
  478.     * } else {
  479.     *    echo $products->message;
  480.     * }
  481.     * ?>
  482.     * </code>
  483.     * If you were to fill in the Developer token in the constructor this would
  484.     * search Amazon.com for all books written by the great American author
  485.     * Frank Herbert.
  486.     *
  487.     * @access public
  488.     * @param  string $author The author you are searching for.
  489.     * @param  interger $page Which page of products to retrieve. Defaults to
  490.     *                        the first.
  491.     * @return array The array of products retrieved by the query.
  492.     */
  493.     function searchAuthor($author, $page = 1) {
  494.         return $this->_sendRequest(array('AuthorSearch' => $author, 'mode' => 'books'), $page);
  495.     }
  496.  
  497.     /**
  498.     * Searches Amazon for music by a specified artist.
  499.     *
  500.     * Example:
  501.     * <code>
  502.     * <?php
  503.     * require_once 'PEAR.php';
  504.     * require_once 'Services/Amazon.php';
  505.     * 
  506.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  507.     * $products = $amazon->searchArtist('Dream Theater');
  508.     *
  509.     * if(!PEAR::isError($products)) {
  510.     *    var_dump($products);
  511.     * } else {
  512.     *    echo $products->message;
  513.     * }
  514.     * ?>
  515.     * </code>
  516.     * If you were to fill in the Developer token in the constructor this would
  517.     * search Amazon.com for music by American Progressive Metal band Dream
  518.     * Theater.
  519.     *
  520.     * @access public
  521.     * @param  string $artist The artist you are searching for.
  522.     * @param  interger $page Which page of products to retrieve. Defaults to
  523.     *                        the first.
  524.     * @return array The array of products retrieved by the query.
  525.     */
  526.     function searchArtist($artist, $page = 1) {
  527.         return $this->_sendRequest(array('ArtistSearch' => $artist, 'mode' => 'music'), $page);
  528.     }
  529.  
  530.     /**
  531.     * Searches Amazon for movies that portrays a specific actor.
  532.     *
  533.     * Example:
  534.     * <code>
  535.     * <?php
  536.     * require_once 'PEAR.php';
  537.     * require_once 'Services/Amazon.php';
  538.     * 
  539.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  540.     * $products = $amazon->searchActor('Samuel L. Jackson');
  541.     *
  542.     * if(!PEAR::isError($products)) {
  543.     *    var_dump($products);
  544.     * } else {
  545.     *    echo $products->message;
  546.     * }
  547.     * ?>
  548.     * </code>
  549.     * If you were to fill in the Developer token in the constructor this would
  550.     * search Amazon.com for DVD movies portraying the American Actor Samuel L.
  551.     * Jackson.
  552.     *
  553.     * @access public
  554.     * @param  string $actor The actor you are searching for.
  555.     * @param  string $mode The section of the site you wish to search in.
  556.     *                      Defaults to DVDs.
  557.     * @param  interger $page Which page of products to retrieve. Defaults to
  558.     *                        the first.
  559.     * @return array The array of products retrieved by the query.
  560.     * @see    getModes()
  561.     */
  562.     function searchActor($actor, $mode = 'dvd', $page = 1) {
  563.         return $this->_sendRequest(array('ActorSearch' => $actor, 'mode' => $mode), $page);
  564.     }
  565.  
  566.     /**
  567.     * Searches Amazon for movies by their given director.
  568.     *
  569.     * Example:
  570.     * <code>
  571.     * <?php
  572.     * require_once 'PEAR.php';
  573.     * require_once 'Services/Amazon.php';
  574.     * 
  575.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  576.     * $products = $amazon->searchDirector('George Lucas');
  577.     *
  578.     * if(!PEAR::isError($products)) {
  579.     *    var_dump($products);
  580.     * } else {
  581.     *    echo $products->message;
  582.     * }
  583.     * ?>
  584.     * </code>
  585.     * If you were to fill in the Developer token in the constructor this would
  586.     * search Amazon.com for DVD movies directed by American film Director
  587.     * George Lucas.
  588.     *
  589.     * @access public
  590.     * @param  string $director The director you are searching for.
  591.     * @param  string $mode The section of the site you wish to search in 
  592.     * @param  interger $page Which page of products to retrieve. Defaults to
  593.     *                        the first.
  594.     * @return array The array of products retrieved by the query.
  595.     * @see    getModes()
  596.     */
  597.     function searchDirector($director, $mode = 'dvd', $page = 1) {
  598.         return $this->_sendRequest(array('DirectorSearch' => $director, 'mode' => $mode), $page);
  599.     }
  600.  
  601.     /**
  602.     * Search Amazon for products from a specific manufacturer.
  603.     *
  604.     * This search is synonymous with searching for a book publisher.
  605.     *
  606.     * Example:
  607.     * <code>
  608.     * <?php
  609.     * require_once 'PEAR.php';
  610.     * require_once 'Services/Amazon.php';
  611.     * 
  612.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  613.     * $products = $amazon->searchManufacturer('New Line', 'dvd');
  614.     *
  615.     * if(!PEAR::isError($products)) {
  616.     *    var_dump($products);
  617.     * } else {
  618.     *    echo $products->message;
  619.     * }
  620.     * ?>
  621.     * </code>
  622.     * If you were to fill in the Developer token in the constructor this would
  623.     * search Amazon.com for DVD movies put out by American film company New
  624.     * Line Cinemas.
  625.     *
  626.     * @access public
  627.     * @param  string $manufacturer The manufacturer you are searching for.
  628.     * @param  string $mode The section of the site you wish to search in.
  629.     * @param  interger $page Which page of products to retrieve. Defaults to
  630.     *                        the first.
  631.     * @return array The array of products retrieved by the query.
  632.     * @see    getModes()
  633.     */
  634.     function searchManufacturer($manufacturer, $mode = 'books', $page = 1) {
  635.         return $this->_sendRequest(array('ManufacturerSearch' => $manufacturer, 'mode' => $mode), $page);
  636.     }
  637.     
  638.     /**
  639.     * Search Amazon for products from a specific book publisher.
  640.     *
  641.     * Example:
  642.     * <code>
  643.     * <?php
  644.     * require_once 'PEAR.php';
  645.     * require_once 'Services/Amazon.php';
  646.     * 
  647.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  648.     * $products = $amazon->searchPublisher('Coriolis', 'books');
  649.     *
  650.     * if(!PEAR::isError($products)) {
  651.     *    var_dump($products);
  652.     * } else {
  653.     *    echo $products->message;
  654.     * }
  655.     * ?>
  656.     * </code>
  657.     * If you were to fill in the Developer token in the constructor this would
  658.     * search Amazon.com for books published by Coriolis Group Books which
  659.     * publish the "Black Book" series of programming books, some of my
  660.     * favorites.
  661.     *
  662.     * @access public
  663.     * @param  string $manufacturer The manufacturer or book publisher you are
  664.     *                              searching for.
  665.     * @param  string $mode The section of the site you wish to search in.
  666.     * @param  interger $page Which page of products to retrieve. Defaults to
  667.     *                        the first.
  668.     * @return array The array of products retrieved by the query.
  669.     */
  670.     function searchPublisher($publisher, $page = 1) {
  671.         return $this->searchManufacturer($publisher, 'books', $page);
  672.     }
  673.  
  674.     /**
  675.     * Retrieves a persons wishlist items given their unique ID.
  676.     *
  677.     * To find a specific wish list ID number, simply travel to the page that
  678.     * contains the list that you are interested in, and look for the list's 13
  679.     * character ID in web page's URL. (It appears after the "/obidos/registry/"
  680.     * string). As an example, the following URL contains the list ID
  681.     * 1QKCTUTWKI1AZ: {@link http://www.amazon.com/exec/obidos/registry/1QKCTUTWKI1AZ}.
  682.     *
  683.     * Example:
  684.     * <code>
  685.     * <?php
  686.     * require_once 'PEAR.php';
  687.     * require_once 'Services/Amazon.php';
  688.     * 
  689.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  690.     * $products = $amazon->searchWishlist('1QKCTUTWKI1AZ');
  691.     *
  692.     * if(!PEAR::isError($products)) {
  693.     *    var_dump($products);
  694.     * } else {
  695.     *    echo $products->message;
  696.     * }
  697.     * ?>
  698.     * </code>
  699.     * If you were to fill in the Developer token in the constructor this would
  700.     * search Amazon.com for my wishlist and return all the products currently
  701.     * on it.
  702.     *
  703.     * @access public
  704.     * @param  string $wishlist The ID of the wishlist you wish to retrieve.
  705.     * @return array The array of products retrieved by the query.
  706.     */
  707.     function searchWishlist($wishlist) {
  708.         return $this->_sendRequest(array('WishlistSearch' => $wishlist), 1);
  709.     }
  710.  
  711.     /**
  712.     * Reformats the results returned from Amazon into something more standardized.
  713.     *
  714.     * @access private
  715.     * @param  array $products The array of products returned from Amazon's web services
  716.     * @return array An array of items that include all basic information about an item.
  717.     */
  718.     function &_processPage($products) {
  719.         $items = array();
  720.  
  721.         foreach($products as $url => $product) {
  722.             $item         = array();
  723.             $item['url']  = $url;
  724.             $item['asin'] = $product->Asin;
  725.             $item['name'] = $product->ProductName;
  726.             $item['type'] = $product->Catalog;
  727.             if (isset($product->Authors)) {
  728.                 if (is_array($product->Authors->Author)) {
  729.                     foreach ($product->Authors->Author as $author) {
  730.                         $item['authors'][] = $author;
  731.                     }
  732.                 } else {
  733.                     $item['authors'][] = $product->Authors->Author;
  734.                 }
  735.             }
  736.             if (isset($product->Artists)) {
  737.                 if (is_array($product->Artists->Artist)) {
  738.                     foreach ($product->Artists->Artist as $artist) {
  739.                         $item['artists'][] = $artist;
  740.                     }
  741.                 } else {
  742.                     $item['artists'][] = $product->Artists->Artist;
  743.                 }
  744.             }
  745.             $item['release']      = isset($product->ReleaseDate) ? $product->ReleaseDate : null;
  746.             $item['manufacturer'] = isset($product->Manufacturer) ? $product->Manufacturer : null;
  747.             $item['imagesmall']   = $product->ImageUrlSmall;
  748.             $item['imagemedium']  = $product->ImageUrlMedium;
  749.             $item['imagelarge']   = $product->ImageUrlLarge;
  750.             $item['listprice']    = isset($product->ListPrice) ? $product->ListPrice : null;
  751.             $item['ourprice']     = isset($product->ListPrice) ? $product->ListPrice : null;
  752.  
  753.             $items[] = $item;
  754.         }
  755.  
  756.         return $items;
  757.     }
  758.  
  759.     /**
  760.     * Sends the request to Amazons Web Services.
  761.     *
  762.     * @access private
  763.     * @param  array   $params An array of url parameters to pass. With the key being the variable for the value.
  764.     * @param  integer $page   Which page of products to retrieve. Defaults to the first.
  765.     * @return mixed Returns a PEAR_Error on error and an array of products on success.
  766.     */
  767.     function &_sendRequest($params = array(), $page = 1) {
  768.         if (is_null($this->_token) || is_null($this->_affid)) {
  769.             return PEAR::raiseError('Developers token or Affiliate ID have not been set.');
  770.         }
  771.  
  772.         // Get base url and append all params after url encoding them
  773.         $url = $this->_baseurl . '?locale=' . $this->_locale . '&type=lite&f=xml&t=' . $this->_affid . '&dev-t=' . $this->_token . '&page=' . $page;
  774.         foreach ($params as $key => $value) {
  775.             if(!is_null($value)) {
  776.                 $url .= '&' . $key . '=' . urlencode($value);
  777.             }
  778.         }
  779.  
  780.         // Open up our HTTP_Request and set our User-Agent field then send the
  781.         // request for the URL.
  782.         $http = &new HTTP_Request($url);
  783.         $http->addHeader('User-Agent', 'Services_Amazon/' . $this->getApiVersion());
  784.         $http->sendRequest();
  785.         
  786.         // Retrieve the result and check that its HTTP 200 Ok. Otherwise raise
  787.         // an error.
  788.         if ($http->getResponseCode() != 200) {
  789.             return PEAR::raiseError('Amazon return HTTP ' . $http->getResponseCode());
  790.         }
  791.         $result = $http->getResponseBody();
  792.         
  793.         // Start up the XML_Unserializer and feed it the data received from
  794.         // Amazon.com
  795.         $xml = &new XML_Unserializer(array('complexType' => 'object', 'keyAttribute' => 'url'));
  796.         $xml->unserialize($result, false);
  797.         $data = $xml->getUnserializedData();
  798.         
  799.         // Check to make sure Amazon didn't give us an error. If so raise it.
  800.         if (isset($data->ErrorMsg)) {
  801.             return PEAR::raiseError($data->ErrorMsg);
  802.         }
  803.         
  804.         // Prepare the data to be sent to _processPage
  805.         $data  = get_object_vars($data);
  806.         $pages = isset($data['TotalPages']) ? (int) $data['TotalPages'] : 1;
  807.         unset($data['TotalPages']);
  808.         $totalresults = isset($data['TotalResults']) ? $data['TotalResults'] : count($data);
  809.         unset($data['TotalResults']);
  810.  
  811.         $products = $this->_processPage($data);
  812.         $products['page']  = $page;
  813.         $products['pages'] = $pages;
  814.         $products['totalresults'] = $totalresults;
  815.  
  816.         return $products;
  817.     }
  818. }
  819. ?>
  820.