亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

利用Yahoo! Search API開發(fā)自已的搜索引擎-php版

瀏覽:14日期:2023-12-26 17:31:51

美國東部時間3月1日,雅虎公司聯(lián)合創(chuàng)始人之一的楊致遠(yuǎn)將宣布公司的搜索網(wǎng)絡(luò)將進(jìn)入Web服務(wù)。雅虎公司在www.developer.yahoo.com網(wǎng)站建立了Yahoo Search Developer Network,公司計劃在此紐約舉行的搜索引擎戰(zhàn)略大會(Search Engine Strategies Conference)上推出這一計劃。該網(wǎng)絡(luò)將允許開發(fā)者在雅虎搜索之上建立新的應(yīng)用程序,其中包括圖像、視頻、新聞以及地區(qū)搜索等內(nèi)容。想要使用這項服務(wù)的會員必須先去http://api.search.yahoo.com/webservices/register_application 申請一個自已的ID號,注:每個ID號每天只能搜索5000次。

下面我們看一下,如何用PHP腳本調(diào)用Yahoo! Search API實現(xiàn)搜索的效果,全部腳本如下:

<?php// Yahoo Web Services PHP Example Code// Rasmus Lerdorf// www.mypchelp.cn

$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><?phpfunction done() {?></body></html><?phpexit;}

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 errorsif (!$dom = domxml_open_mem($xml,DOMXML_LOAD_PARSING,$error)) { echo 'XML parse errorn'; 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 servicesfunction 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='http://www.aoyou183.cn/bcjs/${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='http://www.aoyou183.cn/bcjs/${value[Url]}' />n'; break; case 'Cache': echo 'Cache: <a href='http://www.aoyou183.cn/bcjs/${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='http://www.aoyou183.cn/bcjs/$value'>$value</a><br />n'; else echo '<b>$key:</b> $value<br />'; break; } } echo '<hr />n';}

// Create Previous/Next Page linksif($start > 1) echo '<a href='http://www.aoyou183.cn/YahooSearchExample.php'. '?query='.rawurlencode($_REQUEST['query']). '&zip='.rawurlencode($_REQUEST['zip']). '&type='.rawurlencode($_REQUEST['type']). '&start='.($start-10).''>&lt;-Previous Page</a> &nbsp; ';if($last < $res['totalResultsAvailable']) echo '<a href='http://www.aoyou183.cn/YahooSearchExample.php'. '?query='.rawurlencode($_REQUEST['query']). '&zip='.rawurlencode($_REQUEST['zip']). '&type='.rawurlencode($_REQUEST['type']). '&start='.($last+1).''>Next Page-&gt;</a>';done();?>

有興趣的朋友還可以看一下由[動態(tài)網(wǎng)站制作指南]所制作的ASP版本:http://www.mypchelp.cn/yahoo/

標(biāo)簽: PHP
主站蜘蛛池模板: 草民午夜 | 色婷婷社区| 日韩欧美一中文字幕不卡 | 欧美精品二区 | 日韩精品一区二区在线观看 | 欧美精品毛片 | 久久综合久久综合久久综合 | 麻豆网站在线免费观看 | 永久在线免费观看 | 97超级碰久久久久香蕉人人 | 久久99国产精品久久 | 国产特黄一级片 | 久久亚洲国产成人亚 | 免费a一毛片 | 一级女性全黄生活片免费 | 一本久道久久综合多人 | 美国一级特级毛片片aa视频 | 世界一级毛片 | 午夜激情免费视频 | 欧美日韩一日韩一线不卡 | 性做爰片视频毛片 | 欧美精品一二三区 | 国内在线观看精品免费视频 | 欧美一区二区在线观看免费网站 | 国产欧美在线观看不卡 | 日本免费一级片 | 天天拍久久 | 久久久免费的精品 | 俺去啦网婷婷 | 国内色视频 | 一级做a爰片久久毛片美女 一级做a爰片久久毛片唾 | 久久99精品国产99久久6男男 | 亚洲精品一区二 | 国产成人免费观看在线视频 | 国产伊人久久 | 一级特黄国产高清毛片97看片 | 黄色大片在线免费看 | 成人国产精品免费视频 | 国产黄色毛片 | 激情综合网五月激情 | 亚洲第一综合色 |