How to generate an RSS 2.0 feed

Arancaytar's picture
An RSS feed has one primary purpose: To format information in such a way that it can be read by just about any standards-compliant feed reader.

Therefore, an RSS feed is really not "content" by itself, but rather a mode of output for content that already exists. If you have content that is published periodically in a way similar to news items or announcements, you can publish that content in an RSS feed.

Since the way the content is originally published differs widely (you might want to export the posts of a forum, or a blog, or even just the status of a network node), I'm not going to go into the ways you would get the content from that form into publishable format.

Rather, here is a simple PHP script that assumes the content that should be published is already in a MySQL table (with the appropriate columns: title, author, date, etc), and simply publishes the content according to RSS 2.0 specifications.

Firstly, here is a brief tree outline of how an RSS feed is built up:
What an RSS feed consists of:
- xml-encoding and version
- xml stylesheet (optional, for clean appearance in browser.)
- rss version
- channel:
- title
- url
- description
- language (en-us)
- date of last entry
- number of seconds between updates (common: 300 (5 minutes)
up to 3600 (1 hour)
- image (optional)
- title
- source address
- link address
- width
- height
repeat this block for each entry
| - entry tag
| - guid
| - title
| - author name
| - content
| - date


This is the color-coded PHP code. The complete script is attached as a text file, so you can skip straight to the end and just download it.
<?php
/* php-program for outputting an RSS 2.0 feed
* from entries stored in a MySQL table
*/
/*********************************
* data structure
* the table (entry object) has the following columns (properties).
* - guid (primary key identifier)
* - author (a simple, alphanumeric name)
* - author_email (valid email address - rss 2.0 requires this, so use a default)
* - title
* - content
* - posted (datetime field)
* - link_url
*********************************/
/*************************
* fetch items from table (adapt for column identifiers)
*************************/
function get_items() {
$sql = "select
guid,
author,
author_mail,
title,
content,
unix_timestamp(posted) as posted,
link_url
from items
order by posted desc
limit 0,10;"
;
$res = mysql_query($sql);
while(
$row=mysql_fetch_array($res)) $rows[]=$row;
return
$rows;
}
/*************************
* connect to database (if you have a function for this already, remove this)
*************************/
function dbconnect() {
$server = '';
$user = '';
$pass = '';
$db = '';
$link=mysql_connect($server,$user,$pass);
mysql_select_db($link);
return
$link;
}
$link = dbconnect(); // connect
$items = get_items(); // fetch entries
$update=$items[0]['posted']; // determine newest date
mysql_close($link); // close connection
header("Content-type: application/xml"); // send xml MIME type header
// begin output
<?='<?xml version="1.0" encoding="ISO-8859-1" '
<?=
'<?xml-stylesheet href="/style/feed.css" type="text/css" media="screen"'
<rss version="2.0">
<channel>
<title>Feed-Title</title>
<link>http://feed.url</link>
<description>This is the Feed Description</description>
<language>en-us</language>
<lastBuildDate><?=date("r",$updated)</lastBuildDate>
<ttl>900</ttl>
<image>
<title>Image Title</title>
<url>http://image.net/image.jpg</url>
<link>http://feed.url</link>
<width>88</width>
<height>31</height>
</image>

foreach ($items as $item) { // for each entry
/* write associative array to vars. saves space. */
foreach ($item as $name=>$value) $$name=$value;

<item>
<guid isPermaLink='false'><?=$guid</guid>
<title><?=$title</title>
<author><?=$author <<?=$author_mail></author>
<link><?=$link_url</link>
<description><?=$content</description>
<pubDate><?=date("r",$posted)</pubDate>
</item>

foreach ($item as $name=>$value) $$name=''; // a precaution.
}

</channel>
</rss>
This is the end of the PHP code.
AttachmentSize
rss-en.txt3.12 KB