thesmallprint
thesmallprint

Reputation: 2391

How do you generate an RSS feed?

I've never done it myself, and I've never subscribed to a feed, but it seems that I'm going to have to create one, so I'm wondering. The only way that seems apparent to me is that when the system is updated with a new item (blog post, news item, whatever), a new element should be written to the rss file. Or alternatively have a script that checks for updates to the system a few times a day and writes to the rss file is there is. There's probably a better way of doing it though.

And also, should old elements be removed as new ones are added?

Edit: I should have mentioned, I'm working in PHP, specifically using CodeIgniter, with a mySQL database.

Upvotes: 7

Views: 3731

Answers (8)

Ciaran McNulty
Ciaran McNulty

Reputation: 18848

If you want to generate a feed of elements that already exist in HTML, one option is to modify your HTML markup to use hAtom (http://microformats.org/wiki/hAtom) and then point feed readers through an hAtom->Atom or hAtom->RSS proxy.

Upvotes: 0

Zoredache
Zoredache

Reputation: 39583

For PHP I use feedcreator http://feedcreator.org/

<?php define ('CONFIG_SYSTEM_URL','http://www.domain.tld/');

require_once('feedcreator/feedcreator.class.php');

$feedformat='RSS2.0';

header('Content-type: application/xml');

$rss = new UniversalFeedCreator();
$rss->useCached();
$rss->title = "Item List";
$rss->cssStyleSheet='';
$rss->description = 'this feed';
$rss->link = CONFIG_SYSTEM_URL;
$rss->syndicationURL = CONFIG_SYSTEM_URL.'feed.php';


$articles=new itemList();  // list of stuff
foreach ($articles as $i) {   
    $item = new FeedItem();
    $item->title = sprintf('%s',$i->title);
    $item->link = CONFIG_SYSTEM_URL.'item.php?id='.$i->dbId;
    $item->description = $i->Subject;   
    $item->date = $i->ModifyDate;   
    $item->source = CONFIG_SYSTEM_URL;   
    $item->author = $i->User;
    $rss->addItem($item);
}

print $rss->createFeed($feedformat);

Upvotes: 6

Scott Reynen
Scott Reynen

Reputation: 3550

There are two ways to approach this. The first is to create the RSS document dynamically on request. The second is to write to a static file when a relevant change happens. The latter is faster, but requires a call to update the feed in (likely) many places vs. just one.

With both methods, while you could edit the document with only the changes, it's much simpler to just rewrite the whole document every time with the most recent (10-50) items.

Upvotes: 1

da5id
da5id

Reputation: 9136

I've got good results from Magpie RSS. Set up the included cacheing and all you need to do is write the query to retrieve your data and send the result to Magpie RSS, which then handles the update frequency.

I wouldn't be writing an RSS file, unless your server is under particularly heavy load - one query (or a series of queries that adds to an array) for updated stuff is all you need. Write the query/ies to be sorted by date and then limited by X and you won't need to worry about 'removing old stuff' either.

Upvotes: 2

Greg B
Greg B

Reputation: 14888

An RSS feed is just an XML document formatted in a certain way and linked to from a web page.

Take a look at this page (http://cyber.law.harvard.edu/rss/rss.html) which details the RSS specification, gives example RSS files for you to look at and shows you how to link to them from your site.

How you create the document is up to you. You could write it manually in a text editor, use a language specific XML object, or hit an ASPX/PHP/other page and send the correct content type headers along with the RSS document.

It's not all that hard when you get down to it. good luck!

Upvotes: 0

Nick
Nick

Reputation: 5696

Here's a simple ASP.NET 2 based RSS feed I use as a live bookmark for my localhost dev sites. Might help you get started:

<%@ Page Language="C#" EnableViewState="false" %>
<%@ OutputCache Duration="300" VaryByParam="none" %>

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.DirectoryServices" %>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
    System.Collections.Specialized.StringCollection HideSites = new StringCollection();
    System.Collections.Generic.List<string> Sites = new System.Collections.Generic.List<string>();

    HideSites.Add(@"IISHelp");
    HideSites.Add(@"MSMQ");
    HideSites.Add(@"Printers");

    DirectoryEntry entry = new DirectoryEntry("IIS://LocalHost/W3SVC/1/ROOT");
    foreach (DirectoryEntry site in entry.Children)
    {
        if (site.SchemaClassName == "IIsWebVirtualDir" && !HideSites.Contains(site.Name))
        {
            Sites.Add(site.Name);
        }
    }

    Sites.Sort();

    Response.Clear();
    Response.ContentType = "text/xml";
    XmlTextWriter RSS = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
    RSS.WriteStartDocument();
    RSS.WriteStartElement("rss");
    RSS.WriteAttributeString("version","2.0");
    RSS.WriteStartElement("channel");
    RSS.WriteElementString("title", "Localhost Websites");
    RSS.WriteElementString("link","http://localhost/sitelist.aspx");
    RSS.WriteElementString("description","localhost websites");

    foreach (string s in Sites)
    {
        RSS.WriteStartElement("item");
        RSS.WriteElementString("title", s);
        RSS.WriteElementString("link", "http://localhost/" + s);
        RSS.WriteEndElement();
    }

    RSS.WriteEndElement();
    RSS.WriteEndElement();
    RSS.WriteEndDocument();
    RSS.Flush();
    RSS.Close();
    Response.End();
}

</script>

Upvotes: 0

Eoin Campbell
Eoin Campbell

Reputation: 44268

An RSS Feed is just an XML Document that conforms to a specific schema.

Have a look here

What language are you working in? You can easily script the xml output based on some content in your application. You don't need to explicitly save the file to the file system. It can just be created on the fly

Upvotes: 4

Drew Olson
Drew Olson

Reputation: 3737

I'd say the answer is having an RSS feed be nothing more than another view of your data. This means that your rss feed is simply an xml representation of the data in your database. Readers will then be able to hit that specific url and get back the current information in your application.

Upvotes: 3

Related Questions