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

Source for file Amazon.php

Documentation is available at Amazon.php

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

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