RSS Feed

実験的に開発した小さなプログラムやWebサービスを公開。また、気になった最新IT技術情報をブログ形式でメモ。

Amazon API

Zend Frameworkに付属するデモ:
AmazonのWebサービスを利用するサンプル

Amazonのデータベースから情報を取得して結果を出力します。

動作サンプル : こちら

ソース: /demos/Zend/WebServices/Amazon/amazon-search.php

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