10.3. Generating the RSS

Really Simple Syndication (RSS) is an easy way to notify others of content changes. RSS feeds are used to publicize new blog posts, news events, new product additions to e-commerce sites and more. Visitors typically will subscribe to a feed you make available and are then able to automatically review updates directly from within their favorite aggregator without having to visit the site in a browser.

For historical reasons there are several different RSS formats. They are all XML-based although they aren't necessarily compatible with one another. I've chosen RSS 2.0 to publicize the new posts.

The root element of the feed is rss. Information about the site is placed in a channel section using title, link, and description elements. Any number of item elements follow the channel section, one for each post you want to publicize, each with title, link, and description elements.

Here is public_files/rss.php:

<?php header('Content-Type: text/xml'); ?> <?xml version="1.0"?> <rss version="2.0"> <channel> <title>My Blog</title> <link>http://www.example.com/myBlog</link> <description> My Blog is a place where I write my innermost thoughts and feelings for all the world to read. </description> </channel> <?php // include the 5 most recent entries $query = sprintf('SELECT POST_ID, POST_TITLE, POST_TEXT, ' . 'UNIX_TIMESTAMP(POST_DATE) AS POST_DATE FROM %sBLOG_POST ' . 'ORDER BY POST_DATE DESC', DB_TBL_PREFIX); $result = mysql_query($query, $GLOBALS['DB']); $count = ...

Get PHP and MySQL®: Create-Modify-Reuse now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.