amazon-search.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_Amazon
  17. * @subpackage Demos
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Query Amazon's Product Database
  23. */
  24. /**
  25. * @see Zend_Service_Amazon_Query
  26. */
  27. require_once 'Zend/Service/Amazon/Query.php';
  28. $keywords = '';
  29. $searchFor = '';
  30. if (isset($_POST) && strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
  31. if (isset($_POST['search_term'])) {
  32. $keywords = strip_tags($_POST['search_term']);
  33. }
  34. if (isset($_POST['search_type'])) {
  35. $searchFor = strip_tags($_POST['search_type']);
  36. }
  37. }
  38. ?>
  39. <!DOCTYPE html public "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  40. <html>
  41. <head>
  42. <style type="text/css">
  43. html, body {
  44. margin: 0px;
  45. padding: 0px;
  46. font-family: Tahoma, Verdana, sans-serif;
  47. font-size: 10px;
  48. }
  49. h1 {
  50. margin-top: 0px;
  51. background-color: darkblue;
  52. color: white;
  53. font-size: 16px;
  54. }
  55. form {
  56. text-align: center;
  57. }
  58. label {
  59. font-weight: bold;
  60. }
  61. img {
  62. border: 0px;
  63. padding: 5px;
  64. }
  65. #results {
  66. margin-left: 30px;
  67. }
  68. #results .thumb {
  69. clear: left;
  70. float: left;
  71. }
  72. #results .details {
  73. clear: right;
  74. float: left;
  75. }
  76. h2 {
  77. font-size: 14px;
  78. color: grey;
  79. }
  80. h3 {
  81. clear: both;
  82. font-size: 12px;
  83. }
  84. #poweredby {
  85. clear: both;
  86. }
  87. </style>
  88. </head>
  89. <body>
  90. <h1>Amazon Product Search</h1>
  91. <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
  92. <p>
  93. <label>Search For: <input type="text" name="search_term" value="<?php echo htmlspecialchars($keywords, ENT_QUOTES); ?>"></label>
  94. <label>
  95. in
  96. <select name="search_type">
  97. <?php
  98. $search_types = array (
  99. 0 => 'Apparel',
  100. 1 => 'Baby',
  101. 2 => 'Beauty',
  102. 3 => 'Blended',
  103. 4 => 'Books',
  104. 5 => 'Classical',
  105. 6 => 'DVD',
  106. 7 => 'Digital Music',
  107. 8 => 'Electronics',
  108. 9 => 'Gourmet Food',
  109. 10 => 'Health Personal Care',
  110. 11 => 'Jewelry',
  111. 12 => 'Kitchen',
  112. 13 => 'Magazines',
  113. 14 => 'Merchants',
  114. 15 => 'Miscellaneous',
  115. 16 => 'Music',
  116. 17 => 'Music Tracks',
  117. 18 => 'Musical Instruments',
  118. 19 => 'Office Products',
  119. 20 => 'Outdoor Living',
  120. 21 => 'PC Hardware',
  121. 22 => 'Pet Supplies',
  122. 23 => 'Photo',
  123. 24 => 'Restaurants',
  124. 25 => 'Software',
  125. 26 => 'Sporting Goods',
  126. 27 => 'Tools',
  127. 28 => 'Toys',
  128. 29 => 'VHS',
  129. 30 => 'Video',
  130. 31 => 'Video Games',
  131. 32 => 'Wireless',
  132. 33 => 'Wireless Accessories',
  133. );
  134. foreach ($search_types as $type) {
  135. if ($searchFor == $type) {
  136. ?>
  137. <option value='<?php echo str_replace(" ", "", $type); ?>' selected="selected"><?php echo $type; ?></option>
  138. <?php
  139. } else {
  140. ?>
  141. <option value='<?php echo str_replace(" ", "", $type); ?>'><?php echo $type; ?></option>
  142. <?php
  143. }
  144. }
  145. ?>
  146. </select>
  147. </label>
  148. <input type="submit" value="Search!">
  149. </p>
  150. </form>
  151. <?php
  152. if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
  153. $amazon = new Zend_Service_Amazon_Query("1338XJTNFMTHK413WFR2");
  154. try {
  155. $amazon->category($searchFor)->ResponseGroup('Large')->Keywords($keywords);
  156. $results = $amazon->search();
  157. if ($results->totalResults() > 0) {
  158. echo '<div id="results">';
  159. echo '<h2>Search Results</h2>';
  160. foreach ($results as $result) {
  161. echo "<div>";
  162. echo "<h3>$result->Title</h3>";
  163. if (isset($result->MediumImage)) {
  164. ?>
  165. <div class="thumb">
  166. <a href='<?php echo $result->DetailPageURL; ?>' title='<?php echo $result->Title; ?>'>
  167. <img src='<?php echo $result->MediumImage->Url->getUri(); ?>' />
  168. </a>
  169. </div>
  170. <p class="details" style="height: <?php echo $result->MediumImage->Height; ?>px">
  171. Average Rating: <?php echo $result->AverageRating; ?>
  172. <br />
  173. Total Reviews: <?php echo $result->TotalReviews; ?>
  174. <br />
  175. Price: <?php echo (isset($result->FormattedPrice)) ? $result->FormattedPrice : "Not available"; ?>
  176. <br />
  177. <a href='<?php echo $result->DetailPageURL; ?>'>More Details...</a>
  178. </p>
  179. <?php
  180. } else {
  181. echo "<a href='{$result->DetailPageURL}'>More Details...</a>";
  182. }
  183. echo "</div>";
  184. }
  185. echo '</div>';
  186. }
  187. }
  188. catch (Zend_Service_Exception $e) {
  189. echo '<p style="color: red; font-weight: bold">An error occured, please try again later. (' .$e->getMessage(). ')</p>';
  190. }
  191. }
  192. ?>
  193. <p id="poweredby" style="text-align: center; font-size: 9px;">Powered by the <a href="http://framework.zend.com">Zend Framework</a></p>
  194. </body>
  195. </html>