Home  >  Article  >  Backend Development  >  Create your own RSS client with PHP package_PHP Tutorial

Create your own RSS client with PHP package_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:38:22831browse

RSS,也叫做真正简单聚合(Really Simple Syndication)或者RDF站点摘要(RDF Site Summary),是一个让Web网站向用户发布和聚合最新内容的文件格式。RSS的“feed”用XML来表示;这样做的结果是,它能够被任何具备分析XML文件的客户端读取。现在这样的RSS客户端软件很多,用于Windows和Linux平台的都有,最新版本的Mozilla Firefox和Internet Explorer都允许你订阅所需要的RSS feed,以保证你的手头总有最新的信息。

就像很多优秀的编程语言一样,PHP通过PEAR XML_RSS程序包对读取和创建RSS feed提供了支持。这个程序包是一个预先编译的代码库,它可以让你从RSS feed里提取信息,并把它们转换成另外一个格式(例如,MySQL数据库或者文本文件),或者如果你想要自定义创建一个能够从多个RSS源收集信息的Web页面。

在本文里,我将讲解后面一种情况,告诉你如何使用PEAR XML_RSS包,把来自多个RSS feed的新闻标题集成到一个Web页面上。我现在假设你已经安装好了一个工作正常的Apache和PHP,而且你已经成功地下载和安装了PEAR XML_RSS程序包和依赖关系。


开始吧

现在就让我们从一个简单的例子开始,它将告诉你XML_RSS是如何工作的。首先创建下面的脚本(列表A):

列表A

// 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 headlines
print_r($rss->getItems());
?>

在这里,脚本会读取类定义,然后实例化一个新的XML_RSS()对象。对象的构造函数用来传递元数据的URL——这在本文里就是TechRepublic的RSS feed。然后,调用parse()方法来分析XML和从中提取信息。最后,getItems()方法会返回一个结构清晰的嵌套数组,也就是从feed中提取出来的新闻项目。每一个项目都有一个标题、一段描述、一个发表日期,以及链接到完整文章的URL ,就像下面显示的输出一样(列表B):

列表B

Array
(
    [0] => Array
        (
            [title] => Bump the size of your information store to 75GB (Exchange 2003 Standard Edition only)
            [link] => http://techrepublic.com.com/5100-1035_11-6063252.html?
part=rss&tag=feed&subj=tr
            [description] => In Service Pack 2, the Exchange developers
have provided you with the ability to size the information store to any size you like between 1 and 75 GB, and they chose 18GB as the
default. Here's how to change the size yourself.
            [pubdate] => Fri, 21 Apr 2006 00:00:00 PDT
        )

    [1] => Array
        (
            [title] => Learn the pros and cons of Windows Firewall
            [link] => http://techrepublic.com.com/5100-1009_11-6063367.html?
part=rss&tag=feed&subj=tr
            [description] => Is Windows Firewall up to the task of securing your network? Mike Mullins has
his doubts. In this edition of Security Solutions, he delves into the details of Windows Firewall and weighs its pros and cons.
            [pubdate] => Thu, 20 Apr 2006 13:25:00 PDT
        )

...
)

提取关于feed本身的源信息也是可能的,把调用getItems()改成调用getChannelInfo()就可以了。正如其名字所表示的,这个方法用来返回与feed本身相关的信息,包括题目和描述(如果有的话)。下面就是它的代码(列表C):

列表C

// 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.
)

Use a single feed

As the previous example shows, XML_RSS does a pretty good job of parsing an RSS feed and converting it into a PHP array. Once this array is generated, it is fairly easy to process it into a format suitable for display on a Web site. The following example illustrates this (Listing E):

List E



The latest from TechRepublic:


    // 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 "

  • " . $item['title'] . "
    ";
    echo $item['description'] . " (" . $item['pubdate '] . ")

    ";
    }
    ?>



In this article, the array returned by getItems() is processed using a foreach() loop. Each element of the array is itself an array, and the elements include the news title, description, and publication date. These elements are extracted and formatted into an unsorted HTML list of elements. Figure A shows you an example of this:

Create your own RSS client with PHP package_PHP Tutorial

Array elements

Use multiple feeds

How can one feed be enough? With a little creative code you can add feeds at will! List F is such a piece of code:

List F



// 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
?>
The latest from :


    // print headlines and descriptions
    foreach ($items as $item) {
    echo "

  • " . $item['title'] . "
    ";
                                                                                                                                              . ";
    }
    ?>


}
?>


The modification to the previous example code is simple and clear. I created an array of URLs to different feeds and used a loop to process the array without hard-wired linking the URLs to the feeds in the object constructor. Each iteration of the loop creates a new XML_RSS object with a different source feed; this feed is then processed in the normal way, that is, by calling the parse() and getItems() methods. Other improvements are the use of the getChannelInfo() method, discussed earlier, which is used to dynamically display the feed's name and URL at the top of each title list.

The following is an example of the output result (Figure B):

Create your own RSS client with PHP package_PHP Tutorial

More than one RSS feed


Of course, you can modify this structure to more closely reflect your needs. For example, the script will instantly display all news headlines in each feed; for example, you can change it to only display the first 5 headlines in each feed, just use a for() loop and in the second nesting level Just use a counter. You can also reformat the page layout to display news headlines in a drop-down menu, allowing for a different type of browsing. Give it a try and have fun!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735108.htmlTechArticleRSS, also called Really Simple Syndication or RDF Site Summary, is a A file format that allows Web sites to publish and aggregate the latest content to users...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn