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. echo <<<EOT
  56. <html>
  57. <head>
  58.     <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
  59.     <title>Services_Amazon example</title>
  60. </head>
  61. <body>
  62. <form action="{$_SERVER['PHP_SELF']}" method="get">
  63. <table border="0">
  64. <tr>
  65.     <td>
  66.     <select name="mode">
  67. EOT;
  68.  
  69. $modes Services_Amazon::getModes();
  70. // $modes = array('baby' => 'Baby',
  71. //                'books' => 'Books',
  72. //                'classical' => 'Classical Music',
  73. //                ....
  74. foreach($modes as $k => $v{
  75.     echo '<option value="' $k ($k == $_GET['mode''" selected="selected"' '''">' htmlspecialchars($v'</option>';
  76. }
  77. echo '</select></td>';
  78.  
  79. $keyword htmlspecialchars(safestripslashes($_GET['keyword']));
  80. echo <<< EOT
  81.     <td><input type="text" name="keyword" value="{$keyword}" /></td>
  82.     <td><input type="hidden" name="page" value="1" /></td>
  83.     <td><input type="submit" value="Search" /></td>
  84. </tr>
  85. </table>
  86. </form>
  87. EOT;
  88.  
  89. if (!$_GET{
  90.     echo '</body></html>';
  91.     exit();
  92. }
  93.  
  94. // Validate
  95. if (empty($_GET['keyword'])) {
  96.     report_error('Must search for something');
  97. }
  98. if (!is_numeric($_GET['page']|| $_GET['page'< 1{
  99.     report_error('Invalid page number');
  100. }
  101. if (!isset($modes[$_GET['mode']])) {
  102.     report_error('Invalid mode');
  103. }
  104.  
  105. $amazon &new Services_Amazon($token$associd);
  106.  
  107. $products $amazon->searchKeyword($_GET['keyword']$_GET['mode']$_GET['page']);
  108.  
  109. if (PEAR::isError($products)) {
  110.     report_error($products->message);
  111. }
  112.  
  113.  
  114. echo '<table border="0">';
  115.  
  116. $pages $products['pages'];
  117. $page  $products['page'];
  118.  
  119. disp_links($pages$page);
  120.  
  121. // Display products
  122. for($i = 0; $i < 10; $i++{
  123.     if (!isset($products[$i])) break;
  124.     $product $products[$i];
  125.     $creator '';
  126.     if(is_array($product['authors'])) {
  127.         $creator 'by ' implode(', '$product['authors']);
  128.     elseif(is_array($product['artists'])) {
  129.         $creator 'by ' implode(', '$product['artists']);
  130.     }
  131.     
  132.     $price '';
  133.     if($product['listprice'!= $product['ourprice']{
  134.         $price '<strike>' $product['listprice''</strike> ' $product['ourprice'];
  135.     else {
  136.         $price $product['listprice'];
  137.     }
  138.  
  139.     echo <<<EOT
  140. <tr>
  141.     <td valign="top"><a href="{$product['url']}"><img src="{$product['imagesmall']}" border="0" alt="" /></a></td>
  142.     <td valign="top">
  143.     <b>{$product['name']}</b> $creator<br />
  144.     Category: {$product['type']}<br />
  145.     Release Date: {$product['release']}<br />
  146.     Price: $price<br />
  147.     Manufacturer: {$product['manufacturer']}<br />
  148.     ASIN: {$product['asin']}
  149.     </td>
  150. </tr>
  151. EOT;
  152. }
  153.  
  154. disp_links($pages$page);
  155.  
  156. echo '</table></body></html>';
  157.  
  158. ?>

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