利用Yahoo! Search API开发自已的搜索引擎-php版

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了利用Yahoo! Search API开发自已的搜索引擎-php版脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

    美国东部时间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 are common across all the
// result xml From the web services
function 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 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 "<p>;matched ${res[totalResultsAvailable]},showing $first to $last</p>\n";
if(!empty($res['ResultSetMapUrl'])) {
  echo "<p>Result Set Map: <a href=\"${res[ResultSetMapUrl]}\">${res[ResultSetMapUrl]}</a></p>\n";
}
for($i=0; $i<$res['totalResultsReturned']; $i++) {
  foreach($res[$i] as $key=>$value) {
    switch($key) {
      case 'Thumbnail':
        echo "<img src=\"${value[Url]}\" height=\"${value[Height]}\" width=\"${value[Width]}\" />\n";
        break;
      case 'Cache':
        echo "Cache: <a href=\"${value[Url]}\">${value[Url]}</a> [${value[Size]}]<br />\n";
        break;
      case 'PublishDate':
        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();
?>

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

脚本宝典总结

以上是脚本宝典为你收集整理的利用Yahoo! Search API开发自已的搜索引擎-php版全部内容,希望文章能够帮你解决利用Yahoo! Search API开发自已的搜索引擎-php版所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。