Use Yahoo! Search API development self-developed search engine-PHP version

Source: Internet
Author: User
Tags parse error php example php foreach

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 SearchProgramIncluding 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 the
// 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 "<HR/> \ n ";
}

// 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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.