<?php // Plug-in 78: Get Yahoo! News
/*
* 获取Yahoo!新闻
* 插件说明:
* 插件接受一个搜索串,返回http://news.yahoo.com网页上与此搜索串有关的新闻。
* 若操作成功,则返回一个两元素的数组,其中第一个元素表示返回的新闻的个数,
* 第二个元素是一个子数组,它包含以下信息:
* 标题
* 发布网站
* 日期
* 新闻摘要和说明
* 原新闻的URL地址
* 若操作失败,若返回一个元素的数组,元素的值为FALSE。
* 本插件需要以下参数:
* $search 一个标准的搜索串
*/
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link
$search = "climate change";
echo '<html><head><meta http-equiv="Content-Type" ' .
'content="text/html; charset=utf-8" /></head><body>';
echo '<font face="Verdana" size="2">';
echo "<font face='Arial' size='2'>Recent news results " .
"for: <b>$search</b>:<br /><br />";
$results = PIPHP_GetYahooNews($search);
if (!$results[0]) echo "No news found for $search.";
else
foreach($results[1] as $result)
echo "<a href='$result[4]'>$result[0]</a> ($result[1], " .
"$result[2])<br />$result[3]<br /><br />";
function PIPHP_GetYahooNews($search)
{
// Plug-in 78: Get Yahoo! News
//
// This plug-in takes a search query and returns matching
// news items from news.yahoo.com. Upon success it returns
// a two elemet array, the first being the number of stories
// returned and the second is an array whoise elements are
// each sub-arrays containing these details: 1) News story
// title, 2) Originating site name, 3) Publication date, 4)
// Story summary, and 5) Full story URL. on failure it will
// return a single element array with the value FALSE. It
// requires this argument:
//
// $search: A search query
$reports = array();
$url = 'http://news.search.yahoo.com/news/rss?' .
'ei=UTF-8&fl=0&x=wrt&p=' . rawurlencode($search);
$xml = @file_get_contents($url);
if (!strlen($xml)) return array(FALSE);
$xml = str_replace('<![CDATA[', '', $xml);
$xml = str_replace(']]>', '', $xml);
$xml = str_replace('&', '[ampersand]', $xml);
$xml = str_replace('&', '&', $xml);
$xml = str_replace('[ampersand]', '&', $xml);
$xml = str_replace('<b>', '<b>', $xml);
$xml = str_replace('</b>', '</b>', $xml);
$xml = str_replace('<wbr>', '<wbr>', $xml);
$sxml = simplexml_load_string($xml);
foreach($sxml->channel->item as $item)
{
$flag = FALSE;
$url = $item->link;
$date = date('M jS, g:ia', strtotime($item->pubDate));
$title = $item->title;
$temp = explode(' (', $title);
$title = $temp[0];
$site = str_replace(')', '', $temp[1]);
$desc = $item->description;
for ($j = 0 ; $j < count($reports) ; ++$j)
{
similar_text(strtolower($reports[$j][0]),
strtolower($title), $percent);
if ($percent > 70)
{
$flag = TRUE;
break;
}
}
if (!$flag && strlen($desc))
$reports[] = array($title, $site, $date, $desc, $url);
}
return array(count($reports), $reports);
}
?>