Use Yahoo! Develop your own search engine using SearchAPI-php _ PHP Tutorial

Source: Internet
Author: User
Tags php example
Use Yahoo! Use SearchAPI to develop your own search engine-php version. Yang Zhiyuan, co-founder of Yahoo!, will announce that the company's search network will enter Web services on July 15, March 1. Yahoo! set up the website on www.developer.yahoo.com on January 1, March 1, US East Time. Yang Zhiyuan, co-founder of Yahoo, will announce that the company's search network will enter Web services. 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/

In September March 1, Yang Zhiyuan, a founding member of Yahoo, joined the search network of xuanbu to provide web services. Yahoo! has established Ya on www.developer.yahoo.com...

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.