六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

使用Yahoo! Search API開發自已的搜索引擎-php版

[摘要]美國東部時間3月1日,雅虎公司聯合創始人之一的楊致遠將宣布公司的搜索網絡將進入Web服務。雅虎公司在www.developer.yahoo.com網站建立了Yahoo Search Developer Network,公司計劃在此紐約舉行的搜索引擎戰略大會(Search Engine Strate...

    美國東部時間3月1日,雅虎公司聯合創始人之一的楊致遠將宣布公司的搜索網絡將進入Web服務。雅虎公司在www.developer.yahoo.com網站建立了Yahoo Search Developer Network,公司計劃在此紐約舉行的搜索引擎戰略大會(Search Engine Strategies Conference)上推出這一計劃。該網絡將允許開發者在雅虎搜索之上建立新的應用程序,其中包括圖像、視頻、新聞以及地區搜索等內容。想要使用這項服務的會員必須先去http://api.search.yahoo.com/webservices/register_application  申請一個自已的ID號,注:每個ID號每天只能搜索5000次。

    下面我們看一下,如何用PHP腳本調用Yahoo! Search API實現搜索的效果,全部腳本如下:
  

<?php
// Yahoo Web Services PHP Example Code
// Rasmus Lerdorf
// www.knowsky.com

$appid = 'YahooDemo';
// 在這輸入你申請的ID號

$service = array('image'=>'http://api.search.yahoo.com/ImageSearchService/V1/imageSearch',
                 'local'=>'http://api.local.yahoo.com/LocalSearchService/V1/localSearch',
                 'news'=>'http://api.search.yahoo.com/NewsSearchService/V1/newsSearch',
                 'video'=>'http://api.search.yahoo.com/VideoSearchService/V1/videoSearch',
                 'web'=>'http://api.search.yahoo.com/WebSearchService/V1/webSearch');
?>
<html>
<head><title>PHP Yahoo Web Service Example Code</title></head>
<body>
<form action="YahooSearchExample.php" method="GET">
Search Term: <input type="text" name="query" /><br />
Zip Code: <input type="text" name="zip" /> (for local search)<br />
<input type="submit" value=" Go! " />
<select name="type">
<?php foreach($service as $name => $val) {
    if(!empty($_REQUEST['type']) && $name == $_REQUEST['type'])
      echo "<option SELECTED>$name</option>\n";
    else echo "<option>$name</option>\n";
} ?>
</select>
</form>
<?php
function done() {?>
</body></html>
<?php
exit;
}

if(empty($_REQUEST['query']) !in_array($_REQUEST['type'],array_keys($service))) done();

// Ok, here we go, we have the query and the type of search is valid
// First build the query
$q = '?query='.rawurlencode($_REQUEST['query']);
if(!empty($_REQUEST['zip'])) $q.="&zip=".$_REQUEST['zip'];
if(!empty($_REQUEST['start'])) $q.="&start=".$_REQUEST['start'];
$q .= "&appid=$appid";

// Then send it to the appropriate service
$xml = file_get_contents($service[$_REQUEST['type']].$q);

// Parse the XML and check it for errors
if (!$dom = domxml_open_mem($xml,DOMXML_LOAD_PARSING,$error)) {
  echo "XML parse error\n";
  foreach ($error as $errorline) {
  /* For production use this should obviously be logged to a file instead */
    echo $errorline['errormessage']."<br />\n";
    echo " Node  : " . $errorline['nodename'] . "<br />\n";
    echo " Line  : " . $errorline['line'] . "<br />\n";
    echo " Column : " . $errorline['col'] . "<br />\n";
  }
  done();
}

// Now traverse the DOM with this function
// It is basically a generic parser that turns limited XML into a PHP array
// with only a couple of hardcoded tags which are common across all the
// result xml from the web services
function xml_to_result($dom) {
  $root = $dom->document_element();
  $res['totalResultsAvailable'] = $root->get_attribute('totalResultsAvailable');
  $res['totalResultsReturned'] = $root->get_attribute('totalResultsReturned');
  $res['firstResultPosition'] = $root->get_attribute('firstResultPosition');

  $node = $root->first_child();
  $i = 0;
  while($node) {
    switch($node->tagname) {
      case 'Result':
        $subnode = $node->first_child();
        while($subnode) {
          $subnodes = $subnode->child_nodes();
          if(!empty($subnodes)) foreach($subnodes as $k=>$n) {
            if(empty($n->tagname)) $res[$i][$subnode->tagname] = trim($n->get_content());
            else $res[$i][$subnode->tagname][$n->tagname]=trim($n->get_content());
          }
          $subnode = $subnode->next_sibling();
        }
        break;
      default:
        $res[$node->tagname] = trim($node->get_content());
        $i--;
        break;
    }
    $i++;
    $node = $node->next_sibling();
  } 
  return $res;
}

$res = xml_to_result($dom);

// Ok, now that we have the results in an easy to use format,
// display them.  It's quite ugly because I am using a single
// display loop to display every type and I don't really understand HTML
$first = $res['firstResultPosition'];
$last = $first + $res['totalResultsReturned']-1;
echo "<p>Matched ${res[totalResultsAvailable]}, showing $first to $last</p>\n";
if(!empty($res['ResultSetMapUrl'])) {
  echo "<p>Result Set Map: <a href=\"${res[ResultSetMapUrl]}\">${res[ResultSetMapUrl]}</a></p>\n";
}
for($i=0; $i<$res['totalResultsReturned']; $i++) {
  foreach($res[$i] as $key=>$value) {
    switch($key) {
      case 'Thumbnail':
        echo "<img src=\"${value[Url]}\" height=\"${value[Height]}\" width=\"${value[Width]}\" />\n";
        break;
      case 'Cache':
        echo "Cache: <a href=\"${value[Url]}\">${value[Url]}</a> [${value[Size]}]<br />\n";
        break;
      case 'PublishDate':
        echo "<b>$key:</b> ".strftime('%X %x',$value);
        break;
      default:
        if(stristr($key,'url')) echo "<a href=\"$value\">$value</a><br />\n";
        else echo "<b>$key:</b> $value<br />";
        break;
    }
  }
  echo "<hr />\n";
}

// Create Previous/Next Page links
if($start > 1)
  echo '<a href="/YahooSearchExample.php'.
                       '?query='.rawurlencode($_REQUEST['query']).
                         '&zip='.rawurlencode($_REQUEST['zip']).
                        '&type='.rawurlencode($_REQUEST['type']).
                       '&start='.($start-10).'">&lt;-Previous Page</a> &nbsp; ';
if($last < $res['totalResultsAvailable'])
  echo '<a href="/YahooSearchExample.php'.
                       '?query='.rawurlencode($_REQUEST['query']).
                         '&zip='.rawurlencode($_REQUEST['zip']).
                        '&type='.rawurlencode($_REQUEST['type']).
                       '&start='.($last+1).'">Next Page-&gt;</a>';
done();
?>

有興趣的朋友還可以看一下由[動態網站制作指南]所制作的ASP版本:http://www.knowsky.com/yahoo/




主站蜘蛛池模板: 一级做a爰在线就看 | 亚洲精品乱码国产精品乱码 | 欧美亚洲综合视频 | 中日韩欧美视频 | 欧美影院一区二区三区 | 天天插天天操天天干 | 日韩成人激情 | 欧美一级黄色片免费看 | 欧美视频一区在线 | 日本不卡高清视频 | 午夜看毛片 | 天天操狠狠操夜夜操 | 中文字幕日韩一区二区不卡 | 亚洲大尺度视频 | 亚洲精品高清视频 | 五月天激情婷婷大综合 | 欧美性视频在线播放黑人 | 四虎精品永久免费 | 小草青青免费影视观看 | 天天干夜夜曰 | 中国美女牲交一级毛片 | 日本热久久 | 青草青在线免费视频 | 午夜色视频在线观看 | 色花堂国产精品首页第一页 | 亚洲福利一区 | 日韩毛片在线看 | 日本欧美不卡一区二区三区在线 | 日韩一级生活片 | 欧洲一级做a爱在线观看 | 亚欧三级 | 我要看一级黄色毛片 | 青春草在线免费视频 | 欧美一线视频 | 亚洲系列国产系列 | 天天躁夜夜躁狠狠躁 | 宅男午夜影院 | 日本vs欧美一区二区三区 | 欧美自拍视频在线 | 日本特黄a级高清免费酷网 日本特黄aaaaaaa大片 | 在线五月婷婷 |