插件77:获取Yahoo!股票新闻

<?php // Plug-in 77: Get Yahoo! Stock News
/*
 * 获取Yahoo!股票新闻
 * 插件说明:
 * 插件接受一个股票代码,如AAPL或MSFT,返回该股票的相关新闻和股份信息。
 * 若操作成功,则返回一个三元素数组。
 * 第一个元素是新闻故事的个数。
 * 第二个元素是一个子数组,保存两个URL地址,
 * 第一个是该股票当天的股价小图,第二个是它的大图。
 * 而第三个元素是一个子数组,包含一下详细信息:
 *     标题
 *     发布网站
 *     日期
 *     新闻摘要和详细报道
 *     新闻内容的源URL地址
 * 若操作失败,则返回一个值为FALSE的数组。
 * 本插件需要以下参数:
 * $stock 一个有效地股票代码,如YHOO或JPM.
 */
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$stock   = "AAPL";
$results = PIPHP_GetYahooStockNews($stock);
echo "<font face='Arial' size='2'>Fetching recent news " .
     "stories for: <b>$stock</b>:<br /><br />";

if (!$results[0]) echo "No stories found for $stock.";
else
{
   echo "<a href='http://finance.yahoo.com/q?s=$stock'>".
        "<img src='" . $results[1][0] . "' border='1' />" .
        '</a><br /><br />';

   foreach($results[2] as $result)
      echo "<a href='$result[4]'>$result[0]</a> " .
           "($result[1], $result[2])<br />$result[3]" .
           '<br /><br />';
}

function PIPHP_GetYahooStockNews($stock)
{
   // Plug-in 77:  Get Yahoo! Stock News
   //
   // This plug-in takes a stock ticker symbol and then returns
   // any recent news stories on it from finance.yahoo.com.
   // Upon success it returns a three element array, the first
   // of which is the number of new stories returned, the second
   // is a two element array with a URL to a small (192x96) and
   // a large (512x288) intraday price chart for the stock,  and
   // the third element is an array in which each element
   // contains a sub-array of the following five items for each
   // stock: 1) The title of the news story, 2) The site it came
   // from, 3) The date of the story, 4) The story summary, and
   // 5) The URL of the original story. On failure a single
   // element array with the value FALSE us returned. It requires
   // this argument:
   //
   //    $stock: A stock symbol

   $stock = strtoupper($stock);
   $url   = 'http://finance.yahoo.com';
   $check = @file_get_contents("$url/q?s=$stock");

   if (stristr($check, 'Invalid Ticker Symbol') || $check == '')
      return array(FALSE);

   $reports = array();
   $xml     = file_get_contents("$url/rss/headline?s=$stock");
   $xml     = preg_replace('/<\/?summary>/', '', $xml);
   $xml     = preg_replace('/<\/?image>/',   '', $xml);
   $xml     = preg_replace('/<\/?guid>/',    '', $xml);
   $xml     = preg_replace('/<\/?p?link>/',  '', $xml);
   $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;
      $title = $item->title;
      $temp  = explode(' (', $title);
      $title = $temp[0];
      $site  = str_replace(')', '', $temp[1]);
      $site  = str_replace('at ', '', $site);
      $desc  = $item->description;
      $date  = date('M jS, g:ia',
         strtotime(substr($item->pubDate, 0, 25)));

      for ($j = 0 ; $j < count($reports) ; ++$j)
      {
         similar_text(strtolower($reports[$j][0]),
            strtolower($title), $percent);

         if ($percent > 70)
         {
            $flag = TRUE;
            break;
         }
      }

      if (!$flag && !strstr($title, '[$]') && strlen($desc))
         $reports[] = array($title, $site, $date, $desc, $url);
   }

   $url1 = "http://ichart.finance.yahoo.com/t?s=$stock";
   $url2 = "http://ichart.finance.yahoo.com/b?s=$stock";
   return array(count($reports), array($url1, $url2), $reports);
}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值