PHP Yahoo Web Service Example Code


$service =
array(‘image’=>”,
‘local’=>”,
‘news’=>”,
‘video’=>”,
‘web’=>’;

n”;}

// 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 across all the// result xml from the web
servicesfunction xml_to_result { $root = $dom->document_element();
$res[‘totalResultsAvailable’] =
$root->get_attribute(‘totalResultsAvailable’);
$res[‘totalResultsReturned’] =
$root->get_attribute(‘totalResultsReturned’);
$res[‘firstResultPosition’] =
$root->get_attribute(‘firstResultPosition’);

// Ok, now that we have the results in an easy to use format,// 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

有兴趣的朋友还可以看一下由[动态网站制作指南]所制作的ASP版本:

n”;}for($i=0; $i$value) { switch { case ‘Thumbnail’: echo
新澳门31999 1n”;
break; case ‘Cache’: echo “Cache:
${value[Url]}
[${value[Size]}]
n”; break; case ‘PublishDate’: echo “$key: “.strftime; break;
default: if echo
“$value
n”; else echo “$key: $value
“; break; } } echo “

// Then send it to the appropriate service$xml =
file_get_contents($service[$_REQUEST[‘type’]].$q);

$res = xml_to_result;

$node = $root->first_child(); $i = 0; while { switch { case
‘Result’: $subnode = $node->first_child { $subnodes =
$subnode->child_nodes(); if foreach { if
$res[$i][$subnode->tagname] = trim; else
$res[$i][$subnode->tagname][$n->tagname]=trim; } $subnode =
$subnode->next_sibling(); } break; default:
$res[$node->tagname]新澳门31999 , = trim; $i–; break; } $i++; $node =
$node->next_sibling(); } return $res;}

下面我们看一下,如何用PHP脚本调用Yahoo! Search
API实现搜索的效果,全部脚本如下:

// Create Previous/Next Page linksif echo ”;if($last <
$res[‘totalResultsAvailable’]) echo
‘Next
Page->’;done();?>

美国东部时间3月1日,雅虎公司联合创始人之一的杨致远将宣布公司的搜索网络将进入Web服务。雅虎公司在www.developer.yahoo.com网站建立了Yahoo
Search Developer Network,公司计划在此纽约举行的搜索引擎战略大会(Search
Engine Strategies
Conference)上推出这一计划。该网络将允许开发者在雅虎搜索之上建立新的应用程序,其中包括图像、视频、新闻以及地区搜索等内容。想要使用这项服务的会员必须先去
申请一个自已的ID号,注:每个ID号每天只能搜索5000次。

$appid = ‘YahooDemo’;// 在这输入你申请的ID号

Result Set Map:
${res[ResultSetMapUrl]}

// Ok, here we go, we have the query and the type of search is valid//
First build the query$q = ‘?query=’.rawurlencode;if(!empty
$q.=”&zip=”.$_REQUEST[‘zip’];if(!empty
$q.=”&start=”.$_REQUEST[‘start’];$q .= “&appid=$appid”;

if(empty || !in_array($_REQUEST[‘type’],array_keys;

// Parse the XML and check it for errorsif (!$dom =
domxml_open_mem($xml,DOMXML_LOAD_PARSING,$error)) { echo “XML parse
errorn”; foreach { /* For production use this should obviously 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();}

n”;if(!empty($res[‘ResultSetMapUrl’])) { echo “

发表评论

电子邮件地址不会被公开。 必填项已用*标注