Use Yahoo! SearchAPI developed its own search engine-php version on October 11, March 1, Yahoo company co-founder Yang Zhiyuan will announce that the company's search network will enter the Web service. 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:
// 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 ');
?>
PHP Yahoo Web Service Example Code
Function done () {?>
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']."
\ N ";
Echo "Node:". $ errorline ['nodename']."
\ N ";
Echo "Line:". $ errorline ['line']."
\ N ";
Echo "Column:". $ errorline ['col']."
\ 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"
Matched $ {res [totalResultsAvailable]}, showing $ first to $ last
\ N ";
If (! Empty ($ res ['resulsetmapurl']) {
Echo"
Result Set Map: $ {res [ResultSetMapUrl]}
\ 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 :$ {value [Url]} [$ {value [Size]}]
\ N ";
Break;
Case 'hhdate ':
Echo"
$ Key:". Strftime ('% X % X', $ value );
Break;
Default:
If (stristr ($ key, 'URL') echo "$ value
\ N ";
Else echo"
$ Key:$ Value
";
Break;
}
}
Echo" \ N ";
}
// Create Previous/Next Page links
If ($ start> 1)
Echo ''? Query = '. rawurlencode ($ _ REQUEST ['query']).
'& Zip ='. rawurlencode ($ _ REQUEST ['Zip']).
'& Type ='. rawurlencode ($ _ REQUEST ['type']).
'& Start ='. ($ start-10). '"> <-Previous Page ';
If ($ last <$ res ['totalresultsavailable'])
Echo ''? Query = '. rawurlencode ($ _ REQUEST ['query']).
'& Zip ='. rawurlencode ($ _ REQUEST ['Zip']).
'& Type ='. rawurlencode ($ _ REQUEST ['type']).
'& Start ='. ($ last + 1). '"> Next Page-> ';
Done ();
?>
Interested friends can also take a look at the [dynamic website Production Guide] produced by ASP Version: http://www.knowsky.com/yahoo/