利用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
// 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).'"><-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();
?>
有兴趣的朋友还可以看一下由[动态网站制作指南]所制作的asp版本:http://www.knowsky.com/yahoo/
推荐阅读