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

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