Use the PHP package to create your own RSS client

Source: Internet
Author: User
RSS, also known as ReallySimpleSyndication or RDFSiteSummary, is a file format that allows a Web site to publish and aggregate the latest content to users.

RSS, also known as Really Simple Syndication or RDF Site Summary, is a file format that allows Web sites to publish and aggregate the latest content to users. The RSS feed is represented in XML. The result is that it can be read by any client that has the XML file for analysis. There are a lot of such RSS client software for both Windows and Linux platforms. The latest versions of Mozilla Firefox and Internet Explorer allow you to subscribe to the desired RSS feed, to ensure that you always have the latest information.

Like many excellent programming languages, PHP supports reading and creating RSS feeds through the PEAR XML_RSS package. This package is a pre-compiled code library that allows you to extract information from RSS feeds and convert them into another format (for example, MySQL database or text file ), or if you want to customize a Web page that can collect information from multiple RSS sources.

In this article, I will explain the next situation and show you how to use the PEAR XML_RSS package to integrate news titles from multiple RSS feeds into one web page. Now I assume that you have installed a working Apache and PHP, and you have successfully downloaded and installed the PEAR XML_RSS package and dependency.


Let's get started.

Now let's start with a simple example, which will show you how XML_RSS works. First, create the following script (List ):

List

// 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 ());
?>

Here, the script reads the class definition and instantiates a new XML_RSS () object. The object's constructor is used to pass the metadata URL-this is the RSS feed of TechRepublic in this article. Then, call the parse () method to analyze XML and extract information from it. Finally, the getItems () method returns a clearly structured nested array, that is, the news item extracted from the feed. Each project has a title, a description, a publication date, and a URL linked to the complete article, just like the output shown below (List B ):

List B

Array
(
[0] => Array
(
[Title] => Bump the size of your information store to 75 GB (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 18 GB as
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 mulrenthas
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
)

...
)

It is also possible to extract the source information about the feed. you can call getItems () to call getChannelInfo. As its name indicates, this method is used to return information related to the feed itself, including the question and description (if any ). The following is its code (list C ):

List 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 ());
?>

The output result (List D) is as follows ):

List D

Array
(
[Title] => TechRepublic.com
[Link] => http://www.techrepublic.com/
[Description] => Real World. Real Time. Real IT.
)

Use a single feed

As shown in the previous example, XML_RSS performs quite well in analyzing RSS feeds and converting them into PHP arrays. Once this array is generated, it is quite easy to process it into a format suitable for displaying on a Web site. The following example illustrates this (list 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 in a foreach () loop. Each element of an array is an array, and its elements include the news title, description, and posting date. These elements are extracted and formatted into an unordered HTML list.

Array element

Use multiple feeds

How can a feed be enough? With a somewhat creative code, you can add a feed at will! List F is a piece of code like this:

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']."
    ";
    Echo $ item ['description']."

    ";
    }
    ?>


}
?>


The modifications to the previous sample code are both simple and clear. I created an array containing URLs pointing to different feeds and used a loop to process the array, instead of joining the URL and feed together in the object constructor. Each iteration of the loop creates a new XML_RSS object with a different source feed. the feed is then processed in the normal way, that is, the parse () and getItems () methods are called. Other improvements are the use of the getChannelInfo () method, which has been discussed earlier. it is used to dynamically display the feed name and URL at the top of each title list.

More than one RSS feed
 
Of course, you can modify this structure to better reflect your needs. For example, the script will instantly display all the news titles in each feed. for example, you can change it to show only the first five titles of each feed. you only need to use () loop and use a counter in the second nested layer. You can also re-format the page layout to display the news title in the drop-down menu, so that you can perform a different type of browsing. Have a good time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.