找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 498|回复: 0
打印 上一主题 下一主题

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

[复制链接]

2500

主题

2513

帖子

7520

积分

论坛元老

Rank: 8Rank: 8

积分
7520
跳转到指定楼层
楼主
发表于 2018-2-14 09:47:46 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

                美国东部时间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实现搜索的效果,全部脚本如下:
   
$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');
?>
PHP Yahoo Web Service Example Code

Search Term:
Zip Code: (for local search)

$val) {
    if(!empty($_REQUEST['type']) && $name == $_REQUEST['type'])
      echo "$name\n";
    else echo "$name\n";
} ?>

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']."
\n";
    echo " Node  : " . $errorline['nodename'] . "
\n";
    echo " Line  : " . $errorline['line'] . "
\n";
    echo " Column : " . $errorline['col'] . "
\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 "Matched ${res[totalResultsAvailable]}, showing $first to $last
\n";
if(!empty($res['ResultSetMapUrl'])) {
  echo "Result Set Map: [url=]${res[ResultSetMapUrl]}[/url]
\n";
}
for($i=0; $i$value) {
    switch($key) {
      case 'Thumbnail':
        echo "
[img][/img]
\n";
        break;
      case 'Cache':
        echo "Cache: [url=]${value} [${value[Size]}]
\n";
        break;
      case 'PublishDate':
        echo "$key: ".strftime('%X %x',$value);
        break;
      default:
        if(stristr($key,'url')) echo "[url=]$value[/url]
\n";
        else echo "$key: $value
";
        break;
    }
  }
  echo "\n";
}
// Create Previous/Next Page links
if($start > 1)
  echo '[url=/YahooSearchExample.php'.
                       '?query='.rawurlencode($_REQUEST['query']).
                         '&zip='.rawurlencode($_REQUEST['zip']).
                        '&type='.rawurlencode($_REQUEST['type']).
                       '&start='.($start-10).'][/url]';
done();
?>
有兴趣的朋友还可以看一下由[动态网站制作指南]所制作的ASP版本:http://www.knowsky.com/yahoo/
            
            
您可能感兴趣的文章:
  • php IIS日志分析搜索引擎爬虫记录程序
  • php网站来路获取代码(针对搜索引擎)
  • PHP获取搜索引擎关键字来源的函数(支持百度和谷歌等搜索引擎)
  • PHP屏蔽蜘蛛访问代码及常用搜索引擎的HTTP_USER_AGENT
  • 使用php显示搜索引擎来的关键词
  • PHP记录搜索引擎蜘蛛访问网站足迹的方法
  • php实现判断访问来路是否为搜索引擎机器人的方法
  • php获取从百度、谷歌等搜索引擎进入网站关键词的方法
  • PHP判断来访是搜索引擎蜘蛛还是普通用户的代码小结
  • PHP自定义函数获取搜索引擎来源关键字的方法
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端