文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页网页设计PHP实例 → 利用Yahoo! Search API开发自已的搜索引擎-php版

利用Yahoo! Search API开发自已的搜索引擎-php版

时间:2009/12/15 11:58:00来源:本站整理作者:我要评论(0)

本篇教你利用Yahoo! Search API开发自已的搜索引擎-php版:

  美国东部时间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

$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).'"><-Previous Page</a>   ';
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-></a>';
done();
?>

相关视频

    没有数据

相关阅读 php输出内容乱码解决方法php批量获取首字母(汉字、数字、英文)我的php文件怎么打开_如何打开php文件的办法破解防盗链图片的php函数php显示错误信息方法如何在IIS7下设置支持PHP程序PHP技巧--通过COM使用ADODB成就PHP高手的五个必经之路

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 我的php文件怎么打开_php运算符怎么写 php 如何生成静态页面的函数PHP生成图片缩略图PHP session常见问题集锦及解决办法PHP实现同步远程Mysql

人气排行 DEDE在文章列表文章没有缩略图的不显示图片php+mysq修改用户密码我的php文件怎么打开_如何打开php文件的办法FCKeditor的配置和使用方法使用dedecms建站教程PHP+Ajax实现分页技术图片存储与浏览一例Linux+Apache+PHP+MySQLPHP生成图片缩略图