利用Yahoo! Search API开发自已的搜索引擎-php版-php进阶-PHP教程-幽默笑话

  • 来源:本站
  • 发布时间:2019-07-08
  • 41已阅读
您现在的位置:首页 >> 现代文学 >> 文章
简介 下面我们看一下,如何用PHP脚本调用Yahoo!SearchAPI实现搜索的效果,全部脚本如下:CODE:php//YahooWebServicesPHPExampleCode//Rasmus

利用Yahoo! Search API开发自已的搜索引擎-php版-php进阶-PHP教程-幽默笑话

下面我们看一下,如何用PHP脚本调用Yahoo!SearchAPI实现搜索的效果,全部脚本如下:CODE:php//YahooWebServicesPHPExampleCode//RasmusLerdorf//$appid=YahooDemo;//在这输入你申请的ID号$service=array(image=http:///ImageSearchService/V1/imageSearch,local=http:///LocalSearchService/V1/localSearch,news=http:///NewsSearchService/V1/newsSearch,video=http:///VideoSearchService/V1/videoSearch,web=http:///WebSearchService/V1/webSearch);htmlheadtitlePHPYahooWebServiceExampleCode/title/headbodyformaction=""method="GET"SearchTerm:inputtype="text"name="query"/br/ZipCode:inputtype="text"name="zip"/(forlocalsearch)br/inputtype="submit"value="Go!"/selectname="type"phpforeach($serviceas$name=$val){if(!empty($_REQUEST[type])$name==$_REQUEST[type])echo"optionSELECTED$name/optionn";elseecho"option$name/optionn";}/select/formphpfunctiondone(){/body/htmlphpexit;}if(empty($_REQUEST[query])||!in_array($_REQUEST[type],array_keys($service)))done();//Ok,herewego,wehavethequeryandthetypeofsearchisvalid//Firstbuildthequery$q=query=.rawurlencode($_REQUEST[query]);if(!empty($_REQUEST[zip]))$q.="zip=".$_REQUEST[zip];if(!empty($_REQUEST[start]))$q.="start=".$_REQUEST[start];$q.="appid=$appid";//Thensendittotheappropriateservice$xml=file_get_contents($service[$_REQUEST[type]].$q);//ParsetheXMLandcheckitforerrorsif(!$dom=domxml_open_mem($xml,DOMXML_LOAD_PARSING,$error)){echo"XMLparseerrorn";foreach($erroras$errorline){/*Forproductionusethisshouldobviouslybeloggedtoafileinstead*/echo$errorline[errormessage]."br/n";echo"Node:".$errorline[nodename]."br/n";echo"Line:".$errorline[line]."br/n";echo"Column:".$errorline[col]."br/n";}done();}//NowtraversetheDOMwiththisfunction//ItisbasicallyagenericparserthatturnslimitedXMLintoaPHParray//withonlyacoupleofhardcodedtagswhicharecommonacrossallthe//resultxmlfromthewebservicesfunctionxml_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){caseResult:$subnode=$node-first_child();while($subnode){$subnodes=$subnode-child_nodes();if(!empty($subnodes))foreach($subnodesas$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,nowthatwehavetheresultsinaneasytouseformat,//squiteuglybecauseIamusingasingle//displaylooptodisplayeverytypeandIdontreallyunderstandHTML$first=$res[firstResultPosition];$last=$first+$res[totalResultsReturned]-1;echo"pMatched${res[totalResultsAvailable]},showing$firstto$last/pn";if(!empty($res[ResultSetMapUrl])){echo"pResultSetMap:ahref="${res[ResultSetMapUrl]}"${res[ResultSetMapUrl]}/a/pn";}for($i=0;$i$res[totalResultsReturned];$i++){foreach($res[$i]as$key=$value){switch($key){caseThumbnail:echo"imgsrc="${value[Url]}"height="${value[Height]}"width="${value[Width]}"/n";break;caseCache:echo"Cache:ahref="${value[Url]}"${value[Url]}/a[${value[Size]}]br/n";break;casePublishDate:echo"b$key:/b".strftime(%X%x,$value);break;default:if(stristr($key,url))echo"ahref="$value"$value/abr/n";elseecho"b$key:/b$valuebr/";break;}}echo"hr/n";}//CreatePrevious/NextPagelinksif($start1)echoahref="/.query=.rawurlencode($_REQUEST[query]).zip=.rawurlencode($_REQUEST[zip]).type=.rawurlencode($_REQUEST[type]).start=.($start-10)."lt;-PreviousPage/anbsp;;if($last$res[totalResultsAvailable])echoahref="/.query=.rawurlencode($_REQUEST[query]).zip=.rawurlencode($_REQUEST[zip]).type=.rawurlencode($_REQUEST[type]).start=.($last+1)."NextPage-gt;/a;done();(非常全面的一个php技术网站,有相当丰富的文章和源代码.)。