供销合作社安插在那伦敦召开的搜寻引擎战略大会(Search
分类:博客热点

?php// Yahoo Web Services PHP Example Code// Rasmus Lerdorf// $appid = ''YahooDemo'';// 在此输入你报名的ID号$service = array(''image''='''', ''local''='''', ''news''='''', ''video''='''', ''web''=''''卡塔尔;?htmlheadtitlePHP Yahoo Web Service Example Code/title/headbodyform action=YahooSearchExample.php method=GETSearch 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/optionn; else echo option$name/optionn;} ?/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 = '

美利坚联邦合众国北部时间一月1日,雅虎公司合伙创办者之一的杨致远将宣布公司的追寻网络将跻身Web服务。雅虎公司在网址构建了Yahoo Search Developer Network,集团安插在这里London进行的检索引擎战术大会(Search Engine Strategies Conference卡塔尔国上分娩这一安插。该网络将同意开辟者在雅虎搜索之上创设新的应用程序,在那之中囊括图像、录制、音信以至所在寻觅等内容。想要使用那项服务的会员必得先去_application 申请叁个自已的ID号,注:每种ID号每一日只好搜索5000次。

下边大家看一下,怎么样用PHP脚本调用Yahoo! Search API达成找出的意义,全体脚本如下:

?php// Yahoo Web Services PHP Example Code// Rasmus Lerdorf//

$appid = 'Yahoo德姆o';// 在这里输入你报名的ID号

$service = array('image'='', 'local'='', 'news'='', 'video'='', 'web'='');?htmlheadtitlePHP Yahoo Web Service Example Code/title/headbodyform 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 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 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 "pMatched ${res[totalResultsAvailable]}, showing $first to $last/p/n";if(!empty($res['ResultSetMapUrl'])) { echo "pResult 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/abr //n"; else echo "b$key:/b $valuebr /"; break; } } echo "hr //n";}

// Create Previous/Next Page linksif($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版本:

本文由威尼斯在线注册平台发布于博客热点,转载请注明出处:供销合作社安插在那伦敦召开的搜寻引擎战略大会(Search

上一篇:以下代码的实现 下一篇:废话非常少说一向上代码
猜你喜欢
热门排行
精彩图文