March 1 Eastern time, one of Yahoo's co-founders Jerry Yang will announce that the company's search network will enter the Web service. Yahoo has established Yahoo search Developer Network on the Www.developer.yahoo.com website, and the company plans to host the search engine strategies in New York. Conference) to launch the program. The network will allow developers to build new applications on top of Yahoo search, including images, videos, news, and regional searches. Members who wish to use this service must first go to http://api.search.yahoo.com/webservices/register_application to request a self-ID number, note: Each ID number can only be searched 5,000 times per day.
Let's take a look at how to call Yahoo! in PHP script The search API implements the effect of searching, and all the scripts are as follows:
Yahoo Web Services PHP Example Code
Rasmus Lerdorf
Www.knowsky.com
$appid = ' Yahoodemo ';
Enter the ID number of your application 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 had 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 should obviously is 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's basically a generic parser that turns limited XML into a PHP arr Ay
//With only a couple of hardcoded tags which is common across all the
//result XML from the Web services
Fu Nction Xml_to_result ($dom) {
$root = $dom->document_element ();
$res [' totalresultsavailable '] = $root Get_attribute (' totalresultsavailable ');
$res [' totalresultsreturned '] = $root->get_attribute (' totalresultsreturned ');
$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. We have the results
Display them. It ' s quite ugly 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 [' totalresultsreturned ']-1;
echo "
Matched ${res[totalresultsavailable]}, showing $first to $last
\ n ";
if (!empty ($res [' Resultsetmapurl '])) {
echo "
Result Set Map: ${res[resultsetmapurl]}
\ n ";
}
for ($i =0; $i < $res [' totalresultsreturned ']; $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 ' publishdate ':
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 look at the ASP version produced by the [Dynamic website production Guide]: http://www.knowsky.com/yahoo/
http://www.bkjia.com/PHPjc/313793.html www.bkjia.com true http://www.bkjia.com/PHPjc/313793.html techarticle March 1 Eastern time, one of Yahoo's co-founders Jerry Yang will announce that the company's search network will enter the Web service. Yahoo Company in www.developer.yahoo.com website established ya ...