Creating an RSS Feed

RSS, which stands for Really Simple Syndication (it used to mean Rich Site Summary or RDF Site Summary), is a way for Web sites to provide listings of the site's content. Normally this list contains at least the titles of articles, plus their descriptions (and by "article," think of any type of content that a site might offer). Users access these feeds using an RSS client (many Web browsers support RSS, as well). If they want to read more of an article, there's a link to click, which takes them to the full Web page. RSS is a great convenience and has become popular for good reasons.

RSS feeds are just XML files that have already-established tags. RSS documents begin with the rss root element, with a mandatory attribute called version. You'll want to use the latest version of RSS for that value, which is 2.0 at this writing. So an RSS document starts with:

<?xml version="1.0"?> <rss version="2.0">

After that, all RSS files contain a single channel element. Nested within this element are others, like title, description, and link, all of which describe the RSS feed. <channel>

<title>Name of the RSS Feed</title> <description>Description of the RSS ^ Feed</description> <link>Link to the Web site</link>

Those three elements are required within channel. There are many optional ones, too, like language (e.g., en-us), copyright, managingEditor (an email address), webMaster (also an email address), and so on. See the formal specifications at www.rssboard.org/ rss-specification for more.

Figure 14.13 My supporting forums.

The channel also contains multiple item elements, each item being a piece of content (an article). The item elements also have title, description, link, and other nested elements. <item>

<title>Article Title</title> <description>Article ^ Description</description> <link>Link to this article</link> </item>

None of the item subelements are required, except that either a title or description must be present. You might also use author (an email address) and pubDate (the article's publication date). This last one is tricky because its value must be in the RFC 822-specified format. If you don't know what that is offhand, it's: Wed, 01 Nov 2006 16:45:23 GMT.

That's really all there is to it! Remember: RSS is just formatted XML. If you understand XML, you can create RSS.

For this example, I'll create something that may be somewhat particular but is tremendously useful. As part of my Web site (www.DMCinsights.com), I use the Phorum software (www.phorum.org) to create a support forum for readers (Figure 14.13). Each book I've written has its own forum, meaning there are quite a few. To simplify looking through all of these forums, I want to create an RSS feed that displays the latest newly created threads across every forum, using the subject and part of the thread's body as the title and description. If you aren't using Phorum or don't care about this feature, you'll only need to change a wee bit of this code to create the RSS feed you do want.

To create an RSS feed:

1. Begin a new document in your text editor or IDE (Script 14.9). <?php # Script 14.9 - forum_rss.php

Script 14.9 This PHP script uses a MySQL query to generate an RSS feed.

of O

608_a script_

3 /* This script will create an RSS feed.

4 * The feed content will be recent Phorum threads.

7 // Send the Content-type header:

8 header('Content-type: text/xml');

10 // Create the initial RSS code:

13 <channel>

14 <title>Larry Ullman&apos;s Recent Forum Threads</title>

15 <description>The most recent threads started in Larry&apos;s supporting book forums.</description>

16 <link>http://www.dmcinsights.com/phorum/</link>

19 // Connect to the database:

20 $dbc = @mysql_connect ('localhost', 'username', 'password') OR die ("</channel>\n</rss>\n");

21 @mysql_select_db('database') or die ("</channel>\n</rss>\n");

24 // Change this query for different sources!

25 $q = 'SELECT message_id, forum_id, subject, LEFT(body, 200), datestamp FROM p5_messages WHERE status=2 and parent_id=0 order by datestamp desc LIMIT 50';

27 // Retrieve the results:

29 while ($row = mysql_fetch_array($r, MYSQL_NUM)) {

31 // Print each record as an item:

33 <title>' . htmlentities($row[2]) . '</title>

34 <description>' . htmlentities($row[3]) . '...</description>

35 <link>http://www.dmcinsights.com/phorum/read.php?' . $row[1] . ',' . $row[0] . '</link>

36 <guid>http://www.dmcinsights.com/phorum/read.php?' . $row[1] . ',' . $row[0] . '</guid>

37 <pubDate>' . date('r', $row[4]) . '</pubDate>

43 // Complete the channel and rss elements:

Figure 14.14 Firefox, which supports RSS, shows the channel element's title and description values at the top of the page.

Figure 14.15 A database connection failure still results in a valid RSS feed, but with no articles (being viewed in Safari on Mac OS X here).

Figure 14.15 A database connection failure still results in a valid RSS feed, but with no articles (being viewed in Safari on Mac OS X here).

2. Send the Content-type header. header('Content-type: text/xml'); This page will have a .php extension, because it's a PHP page that must be properly handled by the Web server. But to create an XML page, a header should be sent with the proper Content-type.

3. Create the initial RSS code. echo '<?xml version="1.0"?> <rss version="2.0"> <channel>

<title>Larry Ullman&apos;s Recent ^ Forum Threads</title> <description>The most recent threads ^ started in Larry&apos;s supporting ^ book forums.</description> <link>http://www.dmcinsights.com/ ^ phorum/</link>

These lines of XML get the ball rolling. To start, there's the XML prolog, required in all XML documents. Next is the rss element and the opening channel tag. Within the channel, three tags are used to help describe this feed (Figure 14.14).

4. Connect to the database.

$dbc = @mysql_connect ('localhost', ^ 'username', 'password') OR die ^ ("</channel>\n</rss>\n"); @mysql_select_db('database') or die ^ ("</channel>\n</rss>\n"); If you want to use another database or resource for your feed (like a text file or different database application), you'll need to change this code. If a database connection couldn't be made, the XML page is completed, resulting in no articles in an RSS reader (Figure 14.15).

continues on next page

5. Define the query.

$q = 'SELECT message_id, forum_id, ^ subject, LEFT(body, 200), datestamp ^ FROM p5_messages WHERE status=2 and ^ parent_id=0 order by datestamp desc ^ LIMIT 50';

Understanding this query requires a knowledge of the Phorum software and its database design. I'm selecting the message_id and forum_id values, which are used in creating the link to a thread. I also select the thread's subject and the first 200 characters in its body, which will be used as the title and description values in the feed. The datetime will be used for the pubDate. If you don't want to (or can't) follow this example exactly, just change this query to retrieve any data that's represented in your Web site. You could even select something from one of the other databases created in this book.

6. Retrieve the results.

7. Print each record as an item. echo '<item>

<title>' . htmlentities($row[2]) . ^ '</title> <description>' . ^ htmlentities($row[3]) . ^ '...</description> <link>http://www.dmcinsights.com/ ^ phorum/read.php?' . $row[1] . ',' . ^ $row[0] . '</link> <guid>http://www.dmcinsights.com/ ^ phorum/read.php?' . $row[1] . ',' . ^ $row[0] . '</guid> <pubDate>' . date('r', $row[4]) . ^ '</pubDate>

This is the most important part of the whole script, where each item is generated. First, you have the opening item tag. Then, there's the title, which is the subject of the forum thread and becomes the title of the article in the feed. After that is the description, which is what will be printed in the feed describing the article. For that value, I use some of the thread's body. For both the title and the description, the retrieved value is run through the htmlentities() function because XML does not allow many characters that might appear. Next is the link element, which is a link to the actual ""article" online. In this case, it's a link to the thread itself in the forum. After that is an element called a guid, which isn't required but is a good idea. This is a unique identifier for each item. The URL, which will be unique for each item, can be used here as well. Finally, there's the pubDate, which needs to be in an exact format. Fortunately, PHP's date() function has a shortcut for this: r. This makes the formatting a lot easier!

continues on next page

8. Complete the while loop. } // End of while loop

9. Complete the channel and rss elements. echo '</channel>

11. Save the file as forum_rss.php, place it in your Web directory, and load it in an application that supports RSS feeds (Figures 14.16 and 14.17).

■ If you want to confirm that you've generated a valid RSS feed, check out http://feedvalidator.org.

■ Because of some perceived issues with RSS, an offshoot format called Atom was created. Meant to define a better standard for feeds, Atom is an open standard (unlike RSS, which is both closed and frozen from further development). Although Atom is worth considering, many of the largest Web sites still use RSS 2.0 for their feeds.

Figure 14.16 Viewing the RSS feed in Safari.
Figure 14.17 Viewing the RSS feed in Firefox.
0 0

Post a comment

  • Receive news updates via email from this site