Notes: Working With RSS in CF

Presented by Pete Freitag.

Really Simple Syndication. Just an XML file. Like WDDX: They're both syndication structures. But RSS is a defined structure, so it makes it easier to consume because you know exactly what the standards should be for it.

Studies have shown that 69% of those surveyed don't use RSS, 27% are unaware of it, but use RSS, and 4% are aware RSS users. So about 1/3 of population use RSS, but it has successfully evaded technical knowledge requirements for people to use it. RSS feed is one file (called a channel) with multiple articles (called items).

Why publish RSS? Can increase traffic; people subscribed will return to your site when their reader notifies them that new info is available. Also more convenient for your readers. What are the downsides? Easier to steal your content when it is in a prestructured RSS feed. Can actually increase your bandwidth too, because readers may check your feed every hour or something.

How used? Portals like Google Homepage, RSS readers (desktop apps, Google Reader), Aggregators (like MXNA, Fullasagoog), RSS to email. Not just for blogs; can be used for news articles, forums, newsletters.

RSS vs. ATOM. RSS 2.0 is simple and gets the job done. ATOM is more complex with some add'l features. Typically, the add'l features are not used. For instance, you can post multiple versions for different content types (e.g. plaintext vs. HTML).

Generating an RSS Feed

Start by resetting any content. Then immediately do the XML header because it MUST be on the first line.

<cfcontent reset="true"><?xml version="1.0" ?>
<cfheader name="Content-Type" value="text/xml">

Nice thing about setting it to "text/xml" is it will actually display the XML when opening in the browser. Nice for viewing the results. Can also choose "application/RSS+XML".

<rss version="2.0">
  <channel>
    <title>My News Feed</title>
    <description>This is my news feed!</description>
    <language>en-us</language>
    <lastBuildDate>Wed, 27 Jun 2007 16:46:00 GMT</lastBuildDate>
    <item>
      <title>This Article Title</title>
      <link>http://you.com/news.cfm?id=1</link>
      <description>My article content.</description>
      <pubDate>Wed, 27 Jun 2007 16:46:00 GMT</pubDate>
      <guid>http://you.com/news.cfm?id=1</guid>
    </item>
  </channel>
</rss>

Dates in RSS feed must be RFC 822 compliant. Use the GetHttpTimeString() function to display it in the proper format.

So, when doing this with <cfquery>/<cfoutput>, you might do something like:

<rss version="2.0">
  <channel>
    <title>My Feed</title>
    <description>My feed description.</description>
    <language>en-us</language>
    <lastBuildDate>#GetHttpTimeString(Now())#</lastBuildDate>
    <cfoutput query="myArticles">
      <item>
        <title>#myArticles.Title#</title>
        <link>http://you.com/news.cfm?id=#myArticles.ID#</link>
        <description><![CDATA[#xmlFormat(MyArticle.Story)#]]></description>
        <pubDate>#GetHttpTimeString(myArticles.DT)#</pubDate>
        <guid>http://you.com/news.cfm?id=#myArticles.ID#</guid>
      </item>
    </cfoutput>
  </channel>
</rss>

Send your feed to feedvalidator.org to see if it is valid. Helps you get your feed up and running.

You can also use enclosures, which is most used for podcasting. It's like an "attachment" to your feed. Technically, you can only have one enclosure. It may allow multiple enclosures, but because you're not supposed to, this can be problematic.

<enclosure url="http://you.com/audio.mp3" length="12345" type="audio/mpeg" />

Length is in bytes. Can obviously be more than just audio files. Could be a Word or PDF file.

Can also add a <category> tag within the <item> tag. Can add multiple tags. Technorati bases its "tags" on these categories in RSS feeds. Can also add <author> (must be an email). Can also add <source>, where it refers to a source article you may be writing about or rebutting.

More <channel> tags:

  • copyright: Copyright notice.
  • ttl: Time to live. Number of minutes to cache the feed.
  • image: Include an image for the RSS feed.
  • skipDays: Listing of days that the feed will not change.
  • skipHours: Listing of hours where the feed is not changed.

Can see the RSS 2.0 spec for more info.

Feeds in ColdFusion 8

The new <cffeed> tag can create AND parse RSS feeds.

<cffeed action="create"
        name="#feed#"
        outputfile="rss.xml"
        overwrite="true"
        xmlVar="xmlout">

Name will be a struct with the content. xmlVar allows you to put the generated RSS XML into a variable instead of the file.

So create a struct that contains title, description, link, and item which would be an array of structs with the various item elements (title, description, pubDate, link). This way, you can work with familiar ColdFusion structs and arrays when generating this stuff, then just feed it all to <cffeed>.

Parsing feeds is pretty easy.

<cffeed action="read"
        source="http://you.com/rrs/"
        query="feed"
        properties="feedMetaData">

feedMetaData will be the general channel metadata, and the feed variable from the query="" attribute will be a query form of the items.

Problem with the returned query is that it returns a million columns, and the columns you need to use are different for ATOM vs. RSS. It would be nice if there was one common set of fields that are set depending on whether the feed is ATOM or RSS.

So if using RSS, you may do something like:

<h1><cfoutput>#feedMetaData.title#</cfoutput></h1>
<cfoutput query="feed">
  <h2><a href="#feed.rsslink#">#feed.title#</a></h2>
  <p>#feed.description#</p>
</cfoutput>

So <cffeed> is really nice, although problematic due to the different ways it handles ATOM vs. RSS feeds. This isn't really their fault, but it would be nice if there was a black box that took care of the translation between the two feeds.

Leave a Reply

  Theme Brought to you by Directory Journal and Elegant Directory.