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

Source for file amazon_example1.php

Documentation is available at amazon_example1.php

  1. <?php
  2. //
  3. // Example of usage for Services_Amazon
  4. //
  5. // * VERY IMPORTANT *
  6. // YOU NEED TO CHANGE THE DEVELOPERS TOKEN TO SOMETHING OTHER THEN XXXXXXXXXX
  7. // YOU ALSO SHOULD CHANGE THE ASSOSCIATE ID TO YOUR OWN
  8. // * VERY IMPORTANT *
  9.  
  10. require_once 'PEAR.php';
  11. require_once 'Services/Amazon.php';
  12.  
  13. // The Developers Token
  14. $token 'XXXXXXXXXX';
  15. // An Associate ID
  16. $associd 'xxxxx';
  17.  
  18. function safestripslashes($value)
  19. {
  20.     return get_magic_quotes_gpc(stripslashes($value$value;
  21. }
  22.  
  23. function report_error($msg)
  24. {
  25.     echo "<p><i>{$msg}</i><p></body></html>";
  26.     exit();
  27. }
  28.  
  29. // Display previous/next links
  30. function disp_links($pages$page)
  31. {
  32.     if ($pages > 1{
  33.         echo '<tr><td colspan="2">';
  34.         if ($page > 1{
  35.             echo '<a href="' htmlspecialchars($_SERVER['PHP_SELF'.
  36.                  '?mode=' urlencode(safestripslashes($_GET['mode'])) .
  37.                  '&keyword=' urlencode(safestripslashes($_GET['keyword'])) .
  38.                  '&page=' ($page - 1)) .  '">&laquo; Previous Page</a> ';
  39.         }
  40.         echo 'Page ' $page ' of ' $pages ' ';
  41.         if($page $pages{
  42.             echo '<a href="' htmlspecialchars($_SERVER['PHP_SELF'.
  43.                  '?mode=' urlencode(safestripslashes($_GET['mode'])) .
  44.                  '&keyword=' urlencode(safestripslashes($_GET['keyword'])) .
  45.                  '&page=' ($page + 1)) .  '">Next Page &raquo;</a>';
  46.         }
  47.         echo '</td></tr>';
  48.     }
  49. }
  50.  
  51.     echo 'This script was written to work with Services_Amazon 1 API';
  52.     exit();
  53. }
  54.  
  55. $php_self htmlspecialchars($_SERVER['PHP_SELF']);
  56. echo <<<EOT
  57. <html>
  58. <head>
  59.     <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
  60.     <title>Services_Amazon example</title>
  61. </head>
  62. <body>
  63. <form action="{$php_self}" method="get">
  64. <table border="0">
  65. <tr>
  66.     <td>
  67.     <select name="mode">
  68. EOT;
  69.  
  70. $modes Services_Amazon::getModes();
  71. // $modes = array('baby' => 'Baby',
  72. //                'books' => 'Books',
  73. //                'classical' => 'Classical Music',
  74. //                ....
  75. foreach($modes as $k => $v{
  76.     echo '<option value="' $k ($k == $_GET['mode''" selected="selected"' '''">' htmlspecialchars($v'</option>';
  77. }
  78. echo '</select></td>';
  79.  
  80. $keyword htmlspecialchars(safestripslashes($_GET['keyword']));
  81. echo <<< EOT
  82.     <td><input type="text" name="keyword" value="{$keyword}" /></td>
  83.     <td><input type="hidden" name="page" value="1" /></td>
  84.     <td><input type="submit" value="Search" /></td>
  85. </tr>
  86. </table>
  87. </form>
  88. EOT;
  89.  
  90. if (!$_GET{
  91.     echo '</body></html>';
  92.     exit();
  93. }
  94.  
  95. // Validate
  96. if (empty($_GET['keyword'])) {
  97.     report_error('Must search for something');
  98. }
  99. if (!is_numeric($_GET['page']|| $_GET['page'< 1{
  100.     report_error('Invalid page number');
  101. }
  102. if (!isset($modes[$_GET['mode']])) {
  103.     report_error('Invalid mode');
  104. }
  105.  
  106. $amazon &new Services_Amazon($token$associd);
  107.  
  108. $products $amazon->searchKeyword($_GET['keyword']$_GET['mode']$_GET['page']);
  109.  
  110. if (PEAR::isError($products)) {
  111.     report_error($products->message);
  112. }
  113.  
  114.  
  115. echo '<table border="0">';
  116.  
  117. $pages $products['pages'];
  118. $page  $products['page'];
  119.  
  120. disp_links($pages$page);
  121.  
  122. // Display products
  123. for($i = 0; $i < 10; $i++{
  124.     if (!isset($products[$i])) break;
  125.     $product $products[$i];
  126.     $creator '';
  127.     if(is_array($product['authors'])) {
  128.         $creator 'by ' implode(', '$product['authors']);
  129.     elseif(is_array($product['artists'])) {
  130.         $creator 'by ' implode(', '$product['artists']);
  131.     }
  132.     
  133.     $price '';
  134.     if($product['listprice'!= $product['ourprice']{
  135.         $price '<strike>' $product['listprice''</strike> ' $product['ourprice'];
  136.     else {
  137.         $price $product['listprice'];
  138.     }
  139.  
  140.     echo <<<EOT
  141. <tr>
  142.     <td valign="top"><a href="{$product['url']}"><img src="{$product['imagesmall']}" border="0" alt="" /></a></td>
  143.     <td valign="top">
  144.     <b>{$product['name']}</b> $creator<br />
  145.     Category: {$product['type']}<br />
  146.     Release Date: {$product['release']}<br />
  147.     Price: $price<br />
  148.     Manufacturer: {$product['manufacturer']}<br />
  149.     ASIN: {$product['asin']}
  150.     </td>
  151. </tr>
  152. EOT;
  153. }
  154.  
  155. disp_links($pages$page);
  156.  
  157. echo '</table></body></html>';
  158.  
  159. ?>

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