Yang Zhiyuan, co-founder of Yahoo!, will announce that the company's search network will enter Web Services on July 15, March 1. Yahoo! has established the Yahoo Search Developer Network on www.developer.yahoo.com. The company plans to launch this plan at the Search Engine Strategies Conference held in New York. This network will allow developers to create new applications for Yahoo Search, including images, videos, news, and regional searches. Members who want to use this service must first go to the http://api.search.yahoo.com/webservices/register_application to apply for a self-idnumber, Note: Each idnumber can only search 5000 times a day.
Next, let's take a look at how to use a PHP script to call Yahoo! Search by using the Search API. All scripts are as follows:
<? Php
// Yahoo Web Services PHP Example Code
// Rasmus Lerdorf
// Www.knowsky.com
$ Appid = 'yahoodemo ';
// Enter your ID number here
$ 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> <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>
<? Php
Function done () {?>
</Body> <? Php
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 shocould obviusly 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 errors SS all
// Result xml from the web services
Function xml_to_result ($ dom ){
$ Root = $ dom-> document_element ();
$ Res ['totalresultsavailable'] = $ root-> get_attribute ('totalresultsavailable ');
$ Res ['totalresultsreturne'] = $ root-> get_attribute ('totalresultsreturne ');
$ 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 uugly 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 ['totalresultsreturne']-1;
Echo "<p> Matched $ {res [totalResultsAvailable]}, showing $ first to $ last </p> \ n ";
If (! Empty ($ res ['resulsetmapurl']) {
Echo "<p> Result Set Map: <a href = \ "$ {res [ResultSetMapUrl]} \" >$ {res [ResultSetMapUrl]} </a> </p> \ n ";
}
For ($ I = 0; $ I <$ res ['totalresultsreturne']; $ I ++ ){
Foreach ($ res [$ I] as $ key => $ value ){
Switch ($ key ){
Case 'thumbnail ':
Echo " \ n ";
Break;
Case 'cache ':
Echo "Cache: <a href = \ "$ {value [Url]} \" >$ {value [Url]} </a> [$ {value [Size]}] <br/> \ n ";
Break;
Case 'hhdate ':
Echo "<B> $ key: </B>". strftime ('% X % x', $ value );
Break;
Default:
If (stristr ($ key, 'url') echo "<a href = \" $ value \ "> $ value </a> <br/> \ n ";
Else echo "<B> $ key: </B> $ value <br/> ";
Break;
}
}
Echo "}
// Create Previous/Next Page links
If ($ 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 ();
?>
Interested friends can also take a look at the [dynamic website Production Guide] produced by ASP version: http://www.knowsky.com/yahoo/