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: Services_Amazon.php,v 1.1.1.1 2004/08/12 18:30:20 aztek 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://xml.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->_locale  $locale;
  130.         $this->_baseurl $baseurl;
  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.com.
  239.     *
  240.     * Currently only us, uk, and de 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.         $this->_locale $locale;
  249.     }
  250.     
  251.     /**
  252.     * Retrieves an array of modes supported by Amazon.
  253.     *
  254.     * The array is arranged with the shorthand version as the key and the human
  255.     * readable version as its value. Below are the current modes in the list.
  256.     * <pre>
  257.     * baby        - Baby
  258.     * books       - Books
  259.     * classical   - Classical Music
  260.     * dvd         - DVD
  261.     * electronics - Electronics
  262.     * garden      - Outdoor Living
  263.     * kitchen     - Kitchen & Housewares
  264.     * magazines   - Magazines
  265.     * music       - Popular Music
  266.     * pc-hardware - Computers
  267.     * photo       - Camera & Photo
  268.     * software    - Software
  269.     * toys        - Toys & Games
  270.     * universal   - Tools & Hardware
  271.     * vhs         - VHS
  272.     * videogames  - Computer & Video Games
  273.     * </pre>
  274.     *
  275.     * @access public
  276.     * @static
  277.     * @return array An array of modes with the short hand modename to pass to
  278.     *                Amazon as the key and the longer human readable form as the
  279.     *                key's value.
  280.     */
  281.     function getModes({
  282.         return array('baby'        => 'Baby',
  283.                      'books'       => 'Books',
  284.                      'classical'   => 'Classical Music',
  285.                      'dvd'         => 'DVD',
  286.                      'electronics' => 'Electronics',
  287.                      'garden'      => 'Outdoor Living',
  288.                      'kitchen'     => 'Kitchen & Housewares',
  289.                      'magazines'   => 'Magazines',
  290.                      'music'       => 'Popular Music',
  291.                      'pc-hardware' => 'Computers',
  292.                      'photo'       => 'Camera & Photo',
  293.                      'software'    => 'Software',
  294.                      'toys'        => 'Toys & Games',
  295.                      'universal'   => 'Tools & Hardware',
  296.                      'vhs'         => 'VHS',
  297.                      'videogames'  => 'Computer & Video Games');
  298.     }
  299.  
  300.     /**
  301.     * Retrives the information of a product given its unique ASIN code.
  302.     *
  303.     * Amazon Standard Identification Numbers (ASINs) are unique blocks of 10
  304.     * letters and/or numbers that identify items. You can find the ASIN on the
  305.     * item's product information page at Amazon.com.
  306.     *
  307.     * Example:
  308.     * <code>
  309.     * <?php
  310.     * require_once 'PEAR.php';
  311.     * require_once 'Services/Amazon.php';
  312.     *
  313.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  314.     * $products = $amazon->searchAsin('0672325616');
  315.     *
  316.     * if(!PEAR::isError($products)) {
  317.     *     var_dump($products);
  318.     * } else {
  319.     *     echo $products->message;
  320.     * }
  321.     * ?>
  322.     * </code>
  323.     * If you were to fill in the Developer token in the constructor this would
  324.     * return the results for George Schlossnagle's book Advanced PHP
  325.     * Programming.
  326.     *
  327.     * @access public
  328.     * @param  string $asin The Amazon Standard Identification Number (ASIN)
  329.     *                       of the product your searching for
  330.     * @return array The array of products retrieved by the query.
  331.     */
  332.     function searchAsin($asin{
  333.         return $this->_sendRequest(array('AsinSearch' => $asin)1);
  334.     }
  335.  
  336.     /**
  337.     * Retrives the information of a book given its unique ISBN number.
  338.     *
  339.     * International Standard Book Numbers (ISBNs) are unique numbers that
  340.     * identify every book that is published. It is generally located below a
  341.     * barcode on the back of the book. Note: ISBN numbers are synonymous with
  342.     * ASIN numbers as Amazon uses the ISBN for a books ASIN.
  343.     *
  344.     * @access public
  345.     * @param  string $isbn The International Standard Book Number (ISBN) of the
  346.     *                       book you are searching for.
  347.     * @return array The array of products retrieved by the query.
  348.     * @see    searchAsin()
  349.     */
  350.     function searchIsbn($isbn{
  351.         return $this->searchAsin($isbn1);
  352.     }
  353.     
  354.     /**
  355.     * Retrives the information of a product given its unique UPC number.
  356.     *
  357.     * Since Amazon usually carries the latest version of a product it may not
  358.     * be possible to find a product given its UPC even though a later version
  359.     * appears on Amazon.com.
  360.     *
  361.     * @access public
  362.     * @param  string $upc Universal Product Code (UPC) for the product you are
  363.     *                      searching for.
  364.     * @return array The array of products retrieved by the query.
  365.     */
  366.     function searchUpc($upc{
  367.         return $this->_sendRequest(array('UpcSearch' => $upc)1);
  368.     }
  369.     
  370.     /**
  371.     * Searches Amazon.com for a specific keyword.
  372.     *
  373.     * To limit your search to just a specific category such as books then set
  374.     * the $mode param to something other then null. See {@link getModes()} for a list of
  375.     * modes.
  376.     *
  377.     * Example:
  378.     * <code>
  379.     * <?php
  380.     * require_once 'PEAR.php';
  381.     * require_once 'Services/Amazon.php';
  382.     * 
  383.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  384.     * $products = $amazon->searchKeyword('PHP');
  385.     *
  386.     * if(!PEAR::isError($products)) {
  387.     *    var_dump($products);
  388.     * } else {
  389.     *    echo $products->message;
  390.     * }
  391.     * ?>
  392.     * </code>
  393.     * If you were to fill in the Developer token in the constructor this would
  394.     * search Amazon.com for all books about PHP and return the first 10 results.
  395.     * To retrieve more results you would pass a page number.
  396.     *
  397.     * @access public
  398.     * @param  string $keyword The keyword to search for.
  399.     * @param  string $mode The section of the site you wish to search in.
  400.     *                       Defaults to music.
  401.     * @param  interger $page Which page of products to retrieve. Defaults to
  402.     *                         the first.
  403.     * @return array The array of products retrieved by the query.
  404.     * @see    getModes()
  405.     */
  406.     function searchKeyword($keyword$mode = null$page = 1{
  407.         return $this->_sendRequest(array('KeywordSearch' => $keyword'mode' => $mode)$page);
  408.     }
  409.  
  410.     /**
  411.     * Searches Amazon for products similer to the Asin passed to it.
  412.     *
  413.     * To limit your search to just a specific category such as books then set
  414.     * the $mode param to something other then null. See {@link getModes()} for a list of
  415.     * modes. If the $mode param is null it will search all modes.
  416.     *
  417.     * Example:
  418.     * <code>
  419.     * <?php
  420.     * require_once 'PEAR.php';
  421.     * require_once 'Services/Amazon.php';
  422.     * 
  423.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  424.     * $products = $amazon->searchSimilar('0672325616', 'books');
  425.     *
  426.     * if(!PEAR::isError($products)) {
  427.     *    var_dump($products);
  428.     * } else {
  429.     *    echo $products->message;
  430.     * }
  431.     * ?>
  432.     * </code>
  433.     * If you were to fill in the Developer token in the constructor this would
  434.     * search Amazon.com for all books related to George Schlossnagle's book
  435.     * Advanced PHP Programming. To retrieve more results you would pass a page
  436.     * number.
  437.     *
  438.     * @access public
  439.     * @param  string $asin The Asin of the product that is similer to what you
  440.     *                       are searching for.
  441.     * @param  string $mode The section of the site you wish to search in
  442.     * @param  interger $page Which page of products to retrieve. Defaults to
  443.     *                         the first.
  444.     * @return array The array of products retrieved by the query.
  445.     * @see    getModes()
  446.     */
  447.     function searchSimilar($asin$mode = null$page = 1{
  448.         return $this->_sendRequest(array('SimilaritySearch' => $asin'mode' => $mode)$page);
  449.     }
  450.  
  451.     /**
  452.     * Searches Amazon.com for a specific author.
  453.     *
  454.     * Example:
  455.     * <code>
  456.     * <?php
  457.     * require_once 'PEAR.php';
  458.     * require_once 'Services/Amazon.php';
  459.     * 
  460.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  461.     * $products = $amazon->searchAuthor('Frank Herbert');
  462.     *
  463.     * if(!PEAR::isError($products)) {
  464.     *    var_dump($products);
  465.     * } else {
  466.     *    echo $products->message;
  467.     * }
  468.     * ?>
  469.     * </code>
  470.     * If you were to fill in the Developer token in the constructor this would
  471.     * search Amazon.com for all books written by the great American author
  472.     * Frank Herbert.
  473.     *
  474.     * @access public
  475.     * @param  string $author The author you are searching for.
  476.     * @param  interger $page Which page of products to retrieve. Defaults to
  477.     *                         the first.
  478.     * @return array The array of products retrieved by the query.
  479.     */
  480.     function searchAuthor($author$page = 1{
  481.         return $this->_sendRequest(array('AuthorSearch' => $author'mode' => 'books')$page);
  482.     }
  483.  
  484.     /**
  485.     * Searches Amazon for music by a specified artist.
  486.     *
  487.     * Example:
  488.     * <code>
  489.     * <?php
  490.     * require_once 'PEAR.php';
  491.     * require_once 'Services/Amazon.php';
  492.     * 
  493.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  494.     * $products = $amazon->searchArtist('Dream Theater');
  495.     *
  496.     * if(!PEAR::isError($products)) {
  497.     *    var_dump($products);
  498.     * } else {
  499.     *    echo $products->message;
  500.     * }
  501.     * ?>
  502.     * </code>
  503.     * If you were to fill in the Developer token in the constructor this would
  504.     * search Amazon.com for music by American Progressive Metal band Dream
  505.     * Theater.
  506.     *
  507.     * @access public
  508.     * @param  string $artist The artist you are searching for.
  509.     * @param  interger $page Which page of products to retrieve. Defaults to
  510.     *                         the first.
  511.     * @return array The array of products retrieved by the query.
  512.     */
  513.     function searchArtist($artist$page = 1{
  514.         return $this->_sendRequest(array('ArtistSearch' => $artist'mode' => 'music')$page);
  515.     }
  516.  
  517.     /**
  518.     * Searches Amazon for movies that portrays a specific actor.
  519.     *
  520.     * Example:
  521.     * <code>
  522.     * <?php
  523.     * require_once 'PEAR.php';
  524.     * require_once 'Services/Amazon.php';
  525.     * 
  526.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  527.     * $products = $amazon->searchActor('Samuel L. Jackson');
  528.     *
  529.     * if(!PEAR::isError($products)) {
  530.     *    var_dump($products);
  531.     * } else {
  532.     *    echo $products->message;
  533.     * }
  534.     * ?>
  535.     * </code>
  536.     * If you were to fill in the Developer token in the constructor this would
  537.     * search Amazon.com for DVD movies portraying the American Actor Samuel L.
  538.     * Jackson.
  539.     *
  540.     * @access public
  541.     * @param  string $actor The actor you are searching for.
  542.     * @param  string $mode The section of the site you wish to search in.
  543.     *                       Defaults to DVDs.
  544.     * @param  interger $page Which page of products to retrieve. Defaults to
  545.     *                         the first.
  546.     * @return array The array of products retrieved by the query.
  547.     * @see    getModes()
  548.     */
  549.     function searchActor($actor$mode 'dvd'$page = 1{
  550.         return $this->_sendRequest(array('ActorSearch' => $actor'mode' => $mode)$page);
  551.     }
  552.  
  553.     /**
  554.     * Searches Amazon for movies by their given director.
  555.     *
  556.     * Example:
  557.     * <code>
  558.     * <?php
  559.     * require_once 'PEAR.php';
  560.     * require_once 'Services/Amazon.php';
  561.     * 
  562.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  563.     * $products = $amazon->searchDirector('George Lucas');
  564.     *
  565.     * if(!PEAR::isError($products)) {
  566.     *    var_dump($products);
  567.     * } else {
  568.     *    echo $products->message;
  569.     * }
  570.     * ?>
  571.     * </code>
  572.     * If you were to fill in the Developer token in the constructor this would
  573.     * search Amazon.com for DVD movies directed by American film Director
  574.     * George Lucas.
  575.     *
  576.     * @access public
  577.     * @param  string $director The director you are searching for.
  578.     * @param  string $mode The section of the site you wish to search in
  579.     * @param  interger $page Which page of products to retrieve. Defaults to
  580.     *                         the first.
  581.     * @return array The array of products retrieved by the query.
  582.     * @see    getModes()
  583.     */
  584.     function searchDirector($director$mode 'dvd'$page = 1{
  585.         return $this->_sendRequest(array('DirectorSearch' => $director'mode' => $mode)$page);
  586.     }
  587.  
  588.     /**
  589.     * Search Amazon for products from a specific manufacturer.
  590.     *
  591.     * This search is synonymous with searching for a book publisher.
  592.     *
  593.     * Example:
  594.     * <code>
  595.     * <?php
  596.     * require_once 'PEAR.php';
  597.     * require_once 'Services/Amazon.php';
  598.     * 
  599.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  600.     * $products = $amazon->searchManufacturer('New Line', 'dvd');
  601.     *
  602.     * if(!PEAR::isError($products)) {
  603.     *    var_dump($products);
  604.     * } else {
  605.     *    echo $products->message;
  606.     * }
  607.     * ?>
  608.     * </code>
  609.     * If you were to fill in the Developer token in the constructor this would
  610.     * search Amazon.com for DVD movies put out by American film company New
  611.     * Line Cinemas.
  612.     *
  613.     * @access public
  614.     * @param  string $manufacturer The manufacturer you are searching for.
  615.     * @param  string $mode The section of the site you wish to search in.
  616.     * @param  interger $page Which page of products to retrieve. Defaults to
  617.     *                         the first.
  618.     * @return array The array of products retrieved by the query.
  619.     * @see    getModes()
  620.     */
  621.     function searchManufacturer($manufacturer$mode 'books'$page = 1{
  622.         return $this->_sendRequest(array('ManufacturerSearch' => $manufacturer'mode' => $mode)$page);
  623.     }
  624.     
  625.     /**
  626.     * Search Amazon for products from a specific book publisher.
  627.     *
  628.     * Example:
  629.     * <code>
  630.     * <?php
  631.     * require_once 'PEAR.php';
  632.     * require_once 'Services/Amazon.php';
  633.     * 
  634.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  635.     * $products = $amazon->searchPublisher('Coriolis', 'books');
  636.     *
  637.     * if(!PEAR::isError($products)) {
  638.     *    var_dump($products);
  639.     * } else {
  640.     *    echo $products->message;
  641.     * }
  642.     * ?>
  643.     * </code>
  644.     * If you were to fill in the Developer token in the constructor this would
  645.     * search Amazon.com for books published by Coriolis Group Books which
  646.     * publish the "Black Book" series of programming books, some of my
  647.     * favorites.
  648.     *
  649.     * @access public
  650.     * @param  string $manufacturer The manufacturer or book publisher you are
  651.     *                               searching for.
  652.     * @param  string $mode The section of the site you wish to search in.
  653.     * @param  interger $page Which page of products to retrieve. Defaults to
  654.     *                         the first.
  655.     * @return array The array of products retrieved by the query.
  656.     */
  657.     function searchPublisher($publisher$page = 1{
  658.         return $this->searchManufacturer($publisher'books'$page);
  659.     }
  660.  
  661.     /**
  662.     * Retrieves a persons wishlist items given their unique ID.
  663.     *
  664.     * To find a specific wish list ID number, simply travel to the page that
  665.     * contains the list that you are interested in, and look for the list's 13
  666.     * character ID in web page's URL. (It appears after the "/obidos/registry/"
  667.     * string). As an example, the following URL contains the list ID
  668.     * 1QKCTUTWKI1AZ: {@link http://www.amazon.com/exec/obidos/registry/1QKCTUTWKI1AZ}.
  669.     *
  670.     * Example:
  671.     * <code>
  672.     * <?php
  673.     * require_once 'PEAR.php';
  674.     * require_once 'Services/Amazon.php';
  675.     * 
  676.     * $amazon = &new Services_Amazon('XXXXXXXXXXXXXX', 'myassociateid');
  677.     * $products = $amazon->searchWishlist('1QKCTUTWKI1AZ');
  678.     *
  679.     * if(!PEAR::isError($products)) {
  680.     *    var_dump($products);
  681.     * } else {
  682.     *    echo $products->message;
  683.     * }
  684.     * ?>
  685.     * </code>
  686.     * If you were to fill in the Developer token in the constructor this would
  687.     * search Amazon.com for my wishlist and return all the products currently
  688.     * on it.
  689.     *
  690.     * @access public
  691.     * @param  string $wishlist The ID of the wishlist you wish to retrieve.
  692.     * @return array The array of products retrieved by the query.
  693.     */
  694.     function searchWishlist($wishlist{
  695.         return $this->_sendRequest(array('WishlistSearch' => $wishlist)1);
  696.     }
  697.  
  698.     /**
  699.     * Reformats the results returned from Amazon into something more standardized.
  700.     *
  701.     * @access private
  702.     * @param  array $products The array of products returned from Amazon's web services
  703.     * @return array An array of items that include all basic information about an item.
  704.     */
  705.     function &_processPage($products{
  706.         $items = array();
  707.  
  708.         foreach($products as $url => $product{
  709.             $item         = array();
  710.             $item['url']  $url;
  711.             $item['asin'$product->Asin;
  712.             $item['name'$product->ProductName;
  713.             $item['type'$product->Catalog;
  714.             if (isset($product->Authors)) {
  715.                 if (is_array($product->Authors->Author)) {
  716.                     foreach ($product->Authors->Author as $author{
  717.                         $item['authors'][$author;
  718.                     }
  719.                 else {
  720.                     $item['authors'][$product->Authors->Author;
  721.                 }
  722.             }
  723.             if (isset($product->Artists)) {
  724.                 if (is_array($product->Artists->Artist)) {
  725.                     foreach ($product->Artists->Artist as $artist{
  726.                         $item['artists'][$author;
  727.                     }
  728.                 else {
  729.                     $item['artists'][$product->Artists->Artist;
  730.                 }
  731.             }
  732.             $item['release']      $product->ReleaseDate;
  733.             $item['manufacturer'$product->Manufacturer;
  734.             $item['imagesmall']   $product->ImageUrlSmall;
  735.             $item['imagemedium']  $product->ImageUrlMedium;
  736.             $item['imagelarge']   $product->ImageUrlLarge;
  737.             $item['listprice']    $product->ListPrice;
  738.             $item['ourprice']     $product->ListPrice;
  739.  
  740.             $items[$item;
  741.         }
  742.  
  743.         return $items;
  744.     }
  745.  
  746.     /**
  747.     * Sends the request to Amazons Web Services.
  748.     *
  749.     * @access private
  750.     * @param  array   $params An array of url parameters to pass. With the key being the variable for the value.
  751.     * @param  integer $page   Which page of products to retrieve. Defaults to the first.
  752.     * @return mixed Returns a PEAR_Error on error and an array of products on success.
  753.     */
  754.     function &_sendRequest($params = array()$page = 1{
  755.         if (is_null($this->_token|| is_null($this->_affid)) {
  756.             return PEAR::raiseError('Developers token or Affiliate ID have not been set.');
  757.         }
  758.  
  759.         // Get base url and append all params after url encoding them
  760.         $url $this->_baseurl '?locale=' $this->_locale '&type=lite&f=xml&t=' $this->_affid '&dev-t=' $this->_token '&page=' $page;
  761.         foreach ($params as $key => $value{
  762.             if(!is_null($value)) {
  763.                 $url .= '&' $key '=' urlencode($value);
  764.             }
  765.         }
  766.  
  767.         // Open up our HTTP_Request and set our User-Agent field then send the
  768.         // request for the URL.
  769.         $http &new HTTP_Request($url);
  770.         $http->addHeader('User-Agent''Services_Amazon/' $this->getApiVersion());
  771.         $http->sendRequest();
  772.         
  773.         // Retrieve the result and check that its HTTP 200 Ok. Otherwise raise
  774.         // an error.
  775.         if ($http->getResponseCode(!= 200{
  776.             return PEAR::raiseError('Amazon return HTTP ' $http->getResponseCode());
  777.         }
  778.         $result $http->getResponseBody();
  779.         
  780.         // Start up the XML_Unserializer and feed it the data received from
  781.         // Amazon.com
  782.         $xml &new XML_Unserializer(array('complexType' => 'object''keyAttribute' => 'url'));
  783.         $xml->unserialize($resultfalse);
  784.         $data $xml->getUnserializedData();
  785.         
  786.         // Check to make sure Amazon didn't give us an error. If so raise it.
  787.         if (isset($data->ErrorMsg)) {
  788.             return PEAR::raiseError($data->ErrorMsg);
  789.         }
  790.         
  791.         // Prepare the data to be sent to _processPage
  792.         $data  get_object_vars($data);
  793.         $pages = isset($data['TotalPages']? (int) $data['TotalPages': 1;
  794.         $totalresults $data['TotalResults'];
  795.         unset($data['TotalResults']);
  796.         unset($data['TotalPages']);
  797.  
  798.         $products $this->_processPage($data);
  799.         $products['page']  $page;
  800.         $products['pages'$pages;
  801.         $products['totalresults'$totalresults;
  802.  
  803.         return $products;
  804.     }
  805. }
  806. ?>

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