feed
How to generate an RSS 2.0 feed
Submitted by Arancaytar on Wed, 08/30/2006 - 09:40 – No commentsAn 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:
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.
<br /> /* php-program for outputting an RSS 2.0 feed<br /> * from entries stored in a MySQL table<br /> */<br /> /*********************************<br /> * data structure<br /> * the table (entry object) has the following columns (properties).<br /> * - guid (primary key identifier)<br /> * - author (a simple, alphanumeric name)<br /> * - author_email (valid email address - rss 2.0 requires this, so use a default)<br /> * - title<br /> * - content<br /> * - posted (datetime field)<br /> * - link_url<br /> *********************************/<br /> /*************************<br /> * fetch items from table (adapt for column identifiers)<br /> *************************/<br /> function get_items() {<br /> $sql = "select<br /> guid,<br /> author,<br /> author_mail,<br /> title,<br /> content,<br /> unix_timestamp(posted) as posted,<br /> link_url<br /> from items<br /> order by posted desc<br /> limit 0,10;";<br /> $res = mysql_query($sql);<br /> while($row=mysql_fetch_array($res)) $rows[]=$row;<br /> return $rows;<br /> }<br /> /*************************<br /> * connect to database (if you have a function for this already, remove this)<br /> *************************/<br /> function dbconnect() {<br /> $server = '';<br /> $user = '';<br /> $pass = '';<br /> $db = '';<br /> $link=mysql_connect($server,$user,$pass);<br /> mysql_select_db($link);<br /> return $link;<br /> }<br /> $link = dbconnect(); // connect<br /> $items = get_items(); // fetch entries<br /> $update=$items[0]['posted']; // determine newest date<br /> mysql_close($link); // close connection<br /> header("Content-type: application/xml"); // send xml MIME type header<br /> // begin output<br /> '<?xml version="1.0" encoding="ISO-8859-1" ?>'<br /> '<?xml-stylesheet href="/style/feed.css" type="text/css" media="screen"?>'<br /> <rss version="2.0"><br /> <channel><br /> <title>Feed-Title</title></channel></rss></p> <link />http://feed.url <description>This is the Feed Description</description><br /> <language>en-us</language><br /> <lastBuildDate>date("r",$updated)</lastbuilddate><br /> <ttl>900</ttl><br /> <image><br /> <title>Image Title</title><br /> <url>http://image.net/image.jpg</url> <link />http://feed.url <width>88</width><br /> <height>31</height><br /> </image><br /> <br /> foreach ($items as $item) { // for each entry<br /> /* write associative array to vars. saves space. */<br /> foreach ($item as $name=>$value) $$name=$value;<br /> <br /> <item><br /> <guid isPermaLink='false'>$guid</guid><br /> <title>$title</title><br /> <author>$author <$author_mail></?=$author_mail?></author> <link />$link_url <description>$content</description> <pubDate>date("r",$posted)</pubdate> </item><br /> <br /> foreach ($item as $name=>$value) $$name=''; // a precaution.<br /> }<br /> <br /> <br />
- Arancaytar's blog
- Add new comment
- 2364 reads



