日期:2013-04-14  浏览次数:20503 次

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

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

<?php
// Yahoo Web Services PHP Example Code
// Rasmus Lerdorf
// www.knowsky.com

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

$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></head>
<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></html>
<?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 should obviously 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 ar