列表C
<?php
// include class
include ("RSS.php");
// download and parse RSS data
$rss =& new XML_RSS("http://techrepublic.com.com/5150-22-0.xml");
$rss->parse();
// print channel information
print_r($rss->getChannelInfo());
?>
下面是输出结果(列表D):
列表D
Array
(
[title] => TechRepublic.com
[link] => http://www.techrepublic.com/
[description] => Real World. Real Time.Real IT.
)
使用单个feed
正如前面的例子所显示的,XML_RSS在分析RSS feed和将其转换成PHP数组上做得相当好。一旦这个数组被生成,将其处理成为适合在Web网站上显示的格式就相当容易了。下面一个例子就说明了这一点(列表E):
列表E
<html>
<head></head>
<body>
The latest from TechRepublic: <p />
<ul>
<?php
// include class
include ("RSS.php");
// download and parse RSS data
$rss =& new XML_RSS("http://techrepublic.com.com/5150-22-0.xml");
$rss->parse();
// print channel information
foreach ($rss->getItems() as $item) {
echo "<li><a href=""" . $item['link'] . """>" . $item['title'] . "</a><br />";
echo $item['description'] . " (" . $item['pubdate'] . ") <p />";
}
?>
</ul>
</body>
</html>
在本文里,由getItems()返回的数组用foreach()循环来处理。数组的每一个元素本身就是一个数组,而其中的元素包括新闻标题、描述和发表日期。这些元素被提取出来,并被格式化成一个未排序HTML列表的元素。图A向你显示了这样一个例子:
图A |
|
数组元素 |
使用多个feed
一个feed怎么够呢?通过一个有点创意的代码你就可以随意添加feed了!列表F就是这样一段代码:
列表F
<html>
<head></head>
<body>
<?php
// include class
include ("RSS.php");
// set up array of RSS feeds
$feeds = array( "http://techrepublic.com.com/5150-22-0.xml",
"http://news.linux.com/news.rss",
"http://rss.slashdot.org/Slashdot/slashdot");
// retrieve each feed
// get channel information and headlines
foreach ($feeds as $f) {
$rss =& new XML_RSS($f);
$rss->parse();
$info = $rss->getChannelInfo();
$items = $rss->getItems();
// print channel information
?>
<b>The latest from <a href="<?php echo $info['link']; ?>"><?php echo $info['title']; ?></a></b>:
<p />
<ul>
<?php
// print headlines and descriptions
foreach ($items as $item) {
echo "<li><a href=""" . $item['link'] . """>" . $item['title'] . "</a><br />";
echo $item['description'] . "<p />";
}
?>
</ul>
<p />
<?php
}
?>
</body>
</html>
用户评论