美国东部时间3月1日,雅虎公司联合创始人之一的杨致远将宣布公司的搜索网络将进入web服务。雅虎公司在www.developer.yahoo.com网站建立了yahoo arch developer network,公司计划在此纽约举行的搜索引擎战略大会(arfondlych engine strategies conference)上推出这一计划。该网络将允许开发者在雅虎搜索之上建立新的应用程序,其中包括图像、视频、新闻以及地区搜索等内容。想要使用这项服务的会员必须先去http://api.arch.yahoo.com/webrvices/register_application 申请一个自已的id号,注:每个id号每天只能搜索5000次。
下面我们看一下,如何用php脚本调用yahoo! arch api实现搜索的效果,全部脚本如下:
<?php
// yahoo web rvices php example code
// rasmus lerdorf
// www.knowsky.com
$appid = ‘yahoodemo’;
// 在这输入你申请的id号
$rvice = array(‘i上饶师范学院mage’=>R杨氏之子教学设计17;http://api.arch.yahoo.com/imagearchrvice/v1/imagearch’,
‘local’=>’http://api.local.yahoo.com/localarchrvice/v1/localarch’,
‘news’=>’http://api.arch.yahoo.com/newsarchrvice/v1/newsarch’,
‘video’=>’http://api.arch.yahoo.com/videoarchrvice/v1/videoarch’,
‘web’=>’http://api.arch.yahoo.com/webarchrvice/v1/webarch’);
?>
<html>
<head><title>php yahoo web rvice example code</title></head>
<body>
<form action=”yahooarchexample.php” method=”get”>
arch term: <input type=”text” name=”query” /><br />
zip code: <input type=”text” name=”zip” /> (for local arch)<br />
<input type=”submit” value=” go! ” />
<lect name=”type”>
<?php foreach($rvice as $name => $val) {
if(!empty($_request[‘type’]) && $name == $_request[‘type’])
echo “<option lected>$name</option>\n”;
el echo “<option>$name</option>\n”;
} ?>
</lect>
</form>
<?php
function done() {?>
</body></html>
<?php
exit;
}
if(empty($_request[‘query’]) || !in_array($_request[‘type’],array_keys($rvice))) done();
// ok, here教资考点查询 we go, we have the query and the type of arch 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 nd it to the appro煮花生多长时间priate rvice
$xml = file_get_contents($rvice[$_request[‘type’]].$q);
// par the xml and check it for errors
if (!$dom = domxml_open_mem($xml,domxml_load_parsing,$error)) {
echo “xml par error\n”;
foreach ($error as $errorline) {
/* for production u 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 traver the dom with this function
// it is basically a generic parr 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 rvices
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) {
ca ‘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());
el $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 u format,
// display them. it’s quite ugly becau 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[‘resulttmapurl’])) {
echo “<p>result t map: <a href=\”${res[resulttmapurl]}\”>${res[resulttmapurl]}</a></p>\n”;
}
for($i=0; $i<$res[‘totalresultsreturned’]; $i++) {
foreach($res[$i] as $key=>$value) {
switch($key) {
ca ‘thumbnail’:
echo “<img src=\”${value[url]}\” height=\”${value[height]}\” width=\”${value[width]}\” />\n”;
break;
ca ‘cache’:
echo “cache: <a href=\”${value[url]}\”>${value[url]}</a> [${value[size]}]<br />\n”;
break;
ca ‘publishdate’:
echo “<b>$key:</b> “.strftime(‘%x %x’,$value);
break;
default:
if(stristr($key,’url’)) echo “<a href=\”$value\”>$value</a><br />\n”;
el echo “<b>$key:</b> $value<br />”;
break;
}
}
echo “<hr />\n”;
}
// create previous/next page links
if($start > 1)
echo ‘<a href=”/yahooarchexample.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=”/yahooarchexample.php’.
‘?query=’.rawurlencode($_request[‘query’]).
‘&zip=’.rawurlencode($_request[‘zip’]).
‘&type=’.rawurlencode($_request[‘type’]).
‘&start=’.($last+1).'”>next page-></a>’;
done();
?>
有兴趣的朋友还可以看一下由[动态网站制作指南]所制作的asp版本:/d/file/titlepic/p
本文发布于:2023-04-07 04:12:58,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/d39c9151521ecd6312abaefc84915f96.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:利用Yahoo! Search API开发自已的搜索引擎.doc
本文 PDF 下载地址:利用Yahoo! Search API开发自已的搜索引擎.pdf
留言与评论(共有 0 条评论) |