RSS Feed

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

Yahoo API

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

Yahooから情報を取得して結果を出力します。

動作サンプル : こちら

ソース: /demos/Zend/WebServices/Yahoo/yahoo-multi-search.php

  1. < ?php
  2. /**
  3. * Query Yahoo! Web, Image and News searches
  4. */
  5.  
  6. require_once 'Zend/Service/Yahoo.php';
  7.  
  8. if (isset($_POST) && strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
  9.         $keywords = strip_tags($_POST['search_term']);
  10. } else {
  11.     $keywords = '';
  12. }
  13.  
  14. ?>
  15. < !DOCTYPE html public "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  16. <html>
  17. <head>
  18.     <style type="text/css">
  19.         html, body {
  20.             margin: 0px;
  21.             padding: 0px;
  22.             font-family: Tahoma, Verdana, sans-serif;
  23.             font-size: 10px;
  24.         }
  25.  
  26.         h1 {
  27.             margin-top: 0px;
  28.             background-color: darkblue;
  29.             color: white;
  30.             font-size: 16px;
  31.         }
  32.  
  33.         form {
  34.             text-align: center;
  35.         }
  36.  
  37.         label {
  38.             font-weight: bold;
  39.         }
  40.  
  41.         img {
  42.             border: 0px;
  43.             padding: 5px;
  44.         }
  45.  
  46.         #web, #news {
  47.             float: left;
  48.             width: 48%;
  49.             margin-left: 10px;
  50.         }
  51.  
  52.         #image {
  53.             margin: 10px;
  54.             border: 1px dashed grey;
  55.             background-color: lightgrey;
  56.             text-align: center;
  57.         }
  58.  
  59.         h2 {
  60.             font-size: 14px;
  61.             color: grey;
  62.         }
  63.  
  64.         h3 {
  65.             font-size: 12px;
  66.         }
  67.  
  68.         #poweredby {
  69.             clear: both;
  70.         }
  71.     </style>
  72. </head>
  73. <body>
  74.     <h1>Yahoo! Multi-Search</h1>
  75.     <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
  76.         <p>
  77.             <label>Search For: <input type="text" name="search_term" value="<?php echo htmlspecialchars($keywords, ENT_QUOTES); ?/>"></label>
  78.             <input type="submit" value="Search!"/>
  79.         </p>
  80.     </form>
  81. < ?php
  82.     if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
  83.         $yahoo = new Zend_Service_Yahoo('zendtesting');
  84.  
  85.         try {
  86.             $results = $yahoo->imageSearch($keywords, array("results" => 5));
  87.  
  88.             if ($results->totalResults() > 0) {
  89.                 echo '<div id="image">';
  90.                 echo '<h2>Image Search Results</h2>';
  91.                 foreach ($results as $result) {
  92.                     echo "<a href='{$result->ClickUrl}' title='$result->Title'><img src='{$result-/>Thumbnail->Url->getUri()}'></a>";
  93.                 }
  94.                 echo '</div>';
  95.             }
  96.  
  97.  
  98.             $results = $yahoo->webSearch($keywords);
  99.  
  100.             if ($results->totalResults() > 0) {
  101.                 echo '<div id="web">';
  102.                 echo '<h2>Web Search Results</h2>';
  103.                 foreach ($results as $result) {
  104.                     echo "<h3><a href='{$result->ClickUrl}'>{$result->Title}</a></h3>";
  105.                     echo "<p>{$result->Summary} <br /> [<a href='{$result->CacheUrl}'>Cached Version</a>]</p>";
  106.                 }
  107.                 echo '</div>';
  108.             }
  109.  
  110.  
  111.             $results = $yahoo->newsSearch($keywords);
  112.  
  113.             if ($results->totalResults() > 0) {
  114.                 echo '<div id="news">';
  115.                 echo '<h2>News Search Results</h2>';
  116.                 foreach ($results as $result) {
  117.                     echo "<h3><a href='{$result->ClickUrl}'>{$result->Title}</a></h3>";
  118.                     echo "<p>{$result->Summary}</p>";
  119.                 }
  120.                 echo '</div>';
  121.             }
  122.         }
  123.         catch (Zend_Service_Exception $e) {
  124.             echo '<p style="color: red; font-weight: bold">An error occured, please try again later.</p>';
  125.         }
  126.     }
  127. ?>
  128. <p id="poweredby" style="text-align: center; font-size: 9px;">Powered by the <a href="http://framework.zend.com">Zend Framework</a></p>
  129. </body>
  130. </html>
関連記事