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

Source for file Services_Amazon.php

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

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