利用Yahoo!SearchAPI开发自已的搜索引擎-php版_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了利用Yahoo!SearchAPI开发自已的搜索引擎-php版_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5129字,纯文字阅读大概需要8分钟。
内容图文
![利用Yahoo!SearchAPI开发自已的搜索引擎-php版_PHP教程](/upload/InfoBanner/zyjiaocheng/166/f344b89621644211bf31163ba37fecc7.jpg)
下面我们看一下,如何用PHP脚本调用Yahoo! Search API实现搜索的效果,全部脚本如下:
// 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');
?>
function done() {?>
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']."
\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: ${res[ResultSetMapUrl]}
\n";}
for($i=0; $i<$res['totalResultsReturned']; $i++) {
foreach($res[$i] as $key=>$value) {
switch($key) {
case 'Thumbnail':
echo "
break;
case 'Cache':
echo "Cache: ${value[Url]} [${value[Size]}]
\n";
break;
case 'PublishDate':
echo "$key: ".strftime('%X %x',$value);
break;
default:
if(stristr($key,'url')) echo "$value
\n";
else echo "$key: $value
";
break;
}
}
echo "
\n";
}
// Create Previous/Next Page links
if($start > 1)
echo ' '?query='.rawurlencode($_REQUEST['query']).
'&zip='.rawurlencode($_REQUEST['zip']).
'&type='.rawurlencode($_REQUEST['type']).
'&start='.($start-10).'"><-Previous Page ';
if($last < $res['totalResultsAvailable'])
echo ' '?query='.rawurlencode($_REQUEST['query']).
'&zip='.rawurlencode($_REQUEST['zip']).
'&type='.rawurlencode($_REQUEST['type']).
'&start='.($last+1).'">Next Page->';
done();
?>
有兴趣的朋友还可以看一下由[动态网站制作指南]所制作的ASP版本:http://www.knowsky.com/yahoo/
http://www.bkjia.com/PHPjc/313793.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/313793.htmlTechArticle美国东部时间3月1日,雅虎公司联合创始人之一的杨致远将宣布公司的搜索网络将进入Web服务。雅虎公司在www.developer.yahoo.com网站建立了Ya...
内容总结
以上是互联网集市为您收集整理的利用Yahoo!SearchAPI开发自已的搜索引擎-php版_PHP教程全部内容,希望文章能够帮你解决利用Yahoo!SearchAPI开发自已的搜索引擎-php版_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。