介绍
在下面的例子中,我们示范了一个获得一个RSS feed并将其中的一般部分相关数据保存到一个PHP数组中的简单实例,这样这些数据就能方便的用于输出、保存到数据库等等。
Example #1 用Zend_Feed来处理RSS Feed数据
<?php
require_once 'Zend/Feed.php';
// 取得最新的 Slashdot 头条新闻
try {
$slashdotRss = Zend_Feed::import('http://rss.slashdot.org/Slashdot/slashdot');
} catch (Zend_Feed_Exception $e) {
// feed 导入失败
echo "Exception caught importing feed: {$e->getMessage()}\n";
exit;
}
// 初始化保存 channel 数据的数组
$channel = array(
'title' => $slashdotRss->title(),
'link' => $slashdotRss->link(),
'description' => $slashdotRss->description(),
'items' => array()
);
// 循环获得channel的item并存储到相关数组中
foreach ($slashdotRss as $item) {
$channel['items'][] = array(
'title' => $item->title(),
'link' => $item->link(),
'description' => $item->description()
);
}
|