插件78:获取Yahoo!新闻

<?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);
}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值