PHP CookBook Free Open Book

PHP CookBook

Previous Section Next Section

Recipe 12.12 Reading RSS Feeds

12.12.1 Problem

You want to retrieve an RSS feed and look at the items. This allows you to incorporate newsfeeds from multiple web sites into your application.

12.12.2 Solution

Use the PEAR XML_RSS class. Here's an example that reads the RSS feed for the php.announce mailing list:

require 'XML/RSS.php';

$feed = 'http://news.php.net/group.php?group=php.announce&format=rss';

$rss =& new XML_RSS($feed);
$rss->parse();

print "<ul>\n";
foreach ($rss->getItems() as $item) {
    print '<li><a href="' . $item['link'] . '">' . $item['title'] . "</a></li>\n";
}
print "</ul>\n";

12.12.3 Discussion

RSS, which stands for RDF Site Summary, is an easy-to-use headline or article syndication format written in XML.[2] Many news web sites, such as Slashdot and O'Reilly's Meerkat, provide RSS feeds that update whenever new stories are published. Weblogs have also embraced RSS and having an RSS feed for your blog is a standard feature. The PHP web site also publishes RSS feeds for most PHP mailing lists.

[2] RDF stands for Resource Definition Framework. RSS also stands for Rich Site Summary.

Retrieving and parsing a RSS feed is simple:

$feed = 'http://news.php.net/group.php?group=php.announce&format=rss';

$rss =& new XML_RSS($feed);
$rss->parse();

This example makes $rss a new XML_RSS object and sets the feed to the RSS feed for the php.announce mailing list. The feed is then parsed by XML_RSS::parse( ) and stored internally within $rss.

RSS items are then retrieved as an associative array using XML_RSS:getItems( ) :

print "<ul>\n";

foreach ($rss->getItems() as $item) {
    print '<li><a href="' . $item['link'] . '">' . $item['title'] . "</a></li>\n";
}

print "</ul>\n";

This foreach loop creates an unordered list of items with the item title linking back to the URL associated with the complete article, as shown in Figure 12-1. Besides the required title and link fields, an item can have an optional description field that contains a brief write-up about the item.

Figure 12-1. php.announce RSS feed
figs/phpc_1201.gif

Each channel also has an entry with information about the feed, as shown in Figure 12-2. To retrieve that data, call XML_RSS::getChannelInfo( ) :

$feed = 'http://news.php.net/group.php?group=php.announce&format=rss';
$rss =& new XML_RSS($feed);

$rss->parse();

print "<ul>\n";

foreach ($rss->getChannelInfo() as $key => $value) {
    print "<li>$key: $value</li>\n";
}

print "</ul>\n";
Figure 12-2. php.announce RSS channel information
figs/phpc_1202.gif

12.12.4 See Also

Recipe 12.5 for how to process an RSS feed and transform it to HTML; PEAR's XML_RSS class at http://pear.php.net/package-info.php?package=XML_RSS; more information on RSS at http://groups.yahoo.com/group/rss-dev/files/specification.html; O'Reilly Network's Meerkat at http://www.oreillynet.com/meerkat/ .

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Preface
    Chapter 1. Strings
    Chapter 2. Numbers
    Chapter 3. Dates and Times
    Chapter 4. Arrays
    Chapter 5. Variables
    Chapter 6. Functions
    Chapter 7. Classes and Objects
    Chapter 8. Web Basics
    Chapter 9. Forms
    Chapter 10. Database Access
    Chapter 11. Web Automation
    Chapter 12. XML
    12.1 Introduction
    Recipe 12.2 Generating XML Manually
    Recipe 12.3 Generating XML with the DOM
    Recipe 12.4 Parsing XML with the DOM
    Recipe 12.5 Parsing XML with SAX
    Recipe 12.6 Transforming XML with XSLT
    Recipe 12.7 Sending XML-RPC Requests
    Recipe 12.8 Receiving XML-RPC Requests
    Recipe 12.9 Sending SOAP Requests
    Recipe 12.10 Receiving SOAP Requests
    Recipe 12.11 Exchanging Data with WDDX
    Recipe 12.12 Reading RSS Feeds
    Chapter 13. Regular Expressions
    Chapter 14. Encryption and Security
    Chapter 15. Graphics
    Chapter 16. Internationalization and Localization
    Chapter 17. Internet Services
    Chapter 18. Files
    Chapter 19. Directories
    Chapter 20. Client-Side PHP
    Chapter 21. PEAR
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele