Tải bản đầy đủ - 0 (trang)
Hack 68. Program Yahoo! with Perl

Hack 68. Program Yahoo! with Perl

Tải bản đầy đủ - 0trang

OnaWindowssystemwithActivePerl,youcaninstallthese

modulesfromthecommandlinewiththePerlpackage

manager,likethis:





ppminstallLWP-Simple



Theonlyotherpieceyou'llneedisauniqueapplicationIDfrom

Yahoo!,whichyoucanpickupat

http://api.search.yahoo.com/webservices/register_application.



4.3.1.2.TheCode

ThiscodebuildsaYahoo!SearchWebServicesrequestURL

usingthekeywordpassedtoitwhenthescriptisrun.Thenit

parsestheresponseandprintsitoutinareadableformat.Save

thefollowingcodetoafilenamedyahoo_search.pl:

















#!/usr/bin/perl

#yahoo_search.pl

#Acceptsasearchtermandshowsthetopresults.

#Usage:yahoo_search.pl

#

#YoucancreateanAppID,andreadthefulldocumentat

#forYahoo!WebServicesathttp://developer.yahoo.net











usestrict;

useLWP::Simple;

useXML::Simple;















#SetyouruniqueYahoo!ApplicationID

my$appID="insertyourappID";



#Grabtheincomingsearchquery

my$query=join('',@ARGV)ordie"Usage:yahoo_searc











#ConstructaYahoo!SearchQuerywithonlyrequiredop

my$language="en";

my$req_url="http://api.search.yahoo.com/";





























#Maketherequest

my$yahoo_response=get($req_url);











#ParsetheXML

my$xmlsimple=XML::Simple->new();

my$yahoo_xml=$xmlsimple->XMLin($yahoo_response);









#Initializeresultscounter

my$i;























#Loopthroughtheitemsreturned,printingthemout

foreachmy$result(@{$yahoo_xml->{Result}}){



$i++;



my$title=$result->{Title};



my$summary=$result->{Summary};



my$url=$result->{Url};



print"$i.$title\n$summary\n$url\n\n";



}



$req_url.="WebSearchService/V1/webSearch?";

$req_url.="appid=$appID";

$req_url.="&query=$query";

$req_url.="&language=$language";



ThefinalprintcommandsendstheinformationfromYahoo!to

STDOUT.Youcanchangewhatthisscriptshowsbyrearranging

thevariablesandmakingthislastlinemoreorlesscomplex.



4.3.1.3.RunningtheHack



Simplycallthescriptfromthecommandline:





perlyahoo_search.plinsertword



Andbesuretoenclosephrasesormultiplekeywordsinquotes:





perlyahoo_search.pl"insertmultiwordphrase"



Figure4-3showstheYahoo!Searchresultsforthephrase

"minimalistPerl".



Figure4-3.Yahoo!Searchresultsfor"minimalist

Perl"



ThishackusesminimalistPerltodemonstratehowquickly

Yahoo!datacanbeincludedinPerlscripts,andthistechnique

canbeusedasabuildingblockformoreadvancedscripts.In

fact,mostofthePerlscriptsinthisbookusethisbasicmethod



ofaccessingYahoo!SearchWebServices.



Hack69.ProgramYahoo!withPHP5



TakeadvantageofsomeofthelatestfeaturesinPHPto

quicklyaddYahoo!datatoPHP-poweredpages.

TherecursivelynamedPHPHypertextProcessorlanguageisa

popularchoiceforbuildingdynamicwebapplications.Infact,

Yahoo!itselfhasmadePHPitsdevelopmentplatformofchoice

acrossthecompany.ThePHPplatformiscontinuallyevolving,

andthelatestversion(Version5)includesahandyXMLparser

calledSimpleXML.Asthenameimplies,it'seasytoworkwith.

AndaslongastheXMLthatSimpleXMLisparsingisfairly

simple,it'stheperfecttoolforgettingXMLdataintoobjects

PHPcaneasilymanipulate.

Yahoo!SearchWebServicesresponsesdefinitelyqualifyas

simpleXML,andthishackshowshoweasyitistorequestand

parsethisdatawithPHP.You'llneedPHP5forthishack,but

youwon'tneedanyexternalmodules.



4.4.1.TheCode

Savethefollowingcodetoyourwebserverinafilecalled

yahoo_search.php.



Don'tforgettograbauniqueapplicationIDforthisscriptat

http://developer.yahoo.net.




//yahoo_search.php

//Acceptsasearchtermandshowsthetopresults.

//Usage:yahoo_search.php?p=

//

//YoucancreateanAppID,andreadthefulldocumentation

//forYahoo!WebServicesathttp://developer.yahoo.net/

//SetyouruniqueYahoo!ApplicationID

$appID="insertyourappID";

//Grabtheincomingsearchquery,andencodeforaURL

$query=$_GET['p'];

$query=urlencode($query);

if($query==""){



print"usage:yahoo_search.php?p=<Query>";die;

}

//ConstructaYahoo!SearchQuerywithonlyrequiredoptions

$language="en";

$req_url="http://api.search.yahoo.com/";

$req_url.="WebSearchService/V1/webSearch?";

$req_url.="appid=$appID";

$req_url.="&query=$query";

$req_url.="&language=$language";

//Maketherequest

$yahoo_response=file_get_contents($req_url);

//ParsetheXML

$xml=simplexml_load_string($yahoo_response);

//Initializeresultscounter

$i=0;

?>





Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 68. Program Yahoo! with Perl

Tải bản đầy đủ ngay(0 tr)

×