RSS & Atom Feeds Made Easy for Developers

Published: June 25th, 2007 by:

In this entry you will find 2 feed templates ready to work on your TinyButStrong website. All you need to to is include these templates, set some variables, and merge the data block.


The following templates are ready to go after including it in the TBS class. If you require more help or even implementation code, scroll down beyond the template code.

<?xml version="1.0" encoding="[var.info.charset]"?>
<rss version="2.0">
   
   <channel>
      <title>[var.info.title]</title>
      <link>[var.info.link]</link>
      <description>[var.info.description]</description>
      <language>[var.info.language]</language>
      <managingEditor>[var.info.email]</managingEditor>
                <copyright>Copyright [var.info.year]</copyright>
      <generator>[var.info.generator]</generator>
      <pubDate>[var.info.pub_date]</pubDate>
      <ttl>60</ttl>
      <image>
         <link>[var.info.link]</link>
         <title>[var.info.img_title]</title>
         <url>[var.info.img_url]</url>
      </image>
      
      [blkItems;block=begin]
      
      <item>
         <title>[blkItems.title]</title>
         <link>[blkItems.link]</link>
         <comments>[blkItems.comments]</comments>
                        <description><![CDATA[ [blkItems.description] ]]></description>
         <guid isPermaLink="false">[blkItems.guid]</guid>
         <category>[blkItems.category]</category>
         <pubDate>[blkItems.pub_date]</pubDate>
      </item>
      
      [blkItems;block=end]
      
   </channel>
</rss>
<?xml version="1.0" encoding="[var.info.charset]"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="[var.info.lang]">
   <title>[var.info.title]</title>
   <subtitle>[var.info.description]</subtitle>
        <link rel="alternate" type="text/html" href="[var.info.link]"/>
        <link rel="self" type="application/atom+xml" href="[var.info.atom_link]"/>
   <updated>[var.info.updated]</updated>
   <author>
   <name>[var.info.author_name]</name>
   <uri>[var.info.link]</uri>
   <email>[var.info.email]</email>
   </author>
   <id>[var.info.link]</id>
   <rights>Copyright (c) [var.info.year], [var.info.author_name]</rights>
   
   [blkItems;block=begin]
   
   <entry>
      <title>[blkItems.title]</title>
      <link rel="alternate" type="text/html" href="[blkItems.link]" />
      <updated>[blkItems.updated]</updated>
      <published>[blkItems.pub_date]</published>
      <id>[blkItems.link]</id>
      <summary type="text">[blkItems.summery]</summary>
        <content type="html" xml:lang="[blkItems.lang]" xml:base="[blkItems.link]"><![CDATA[
                [blkItems.content]
      ]]></content>
      <author>
         <name>[blkItems.author]</name>
      </author>
   </entry>
   
   [blkItems;block=end]
   
</feed>

Please note that in my examples, I don’t include classes before I define them because I make use if PHP’s __autoload() function. Since you probably don’t, insert the necessary include statement(s) before any object declaration statements. Most of the variables are self-explanatory in this file. Lines 12-23 define fields that are for the general feed, and data that does not pertain the the individual elements. Lines 30-48 take care of the feed items. In my example, I run a MySQL query, and feed that data into a complex array. You can populate that array any way you want as long as you keep those element names the same and keep that MergeBlock function alone online 48.

<?php
   /* make sure that we are sending the correct header */
   header('Content-type: application/rss+xml');
   
   /* create template object */
   $tbs = new clsTinyButStrong();
   
   /* set main template */
   $tbs->LoadTemplate('rss_2_0.xml', false);
   
   /* define information fields */
   $info['charset'] = 'utf-8';
   $info['title'] = '';
   $info['link'] = '';
   $info['description'] = '';
   $info['language'] = 'en';
   $info['email'] = '';
   $info['year'] = '';
   $info['generator'] = '';
   $info['pub_date'] = date('r');
   $info['img_link'] = '';
   $info['img_title'] = '';
   $info['img_url'] = '';
   
   
   /* for this section, you can include your entries through a query (if your column names 
      match up with the fields), or a data array */
   
   /* I'm going to use a data array from a MySQL table.  The following is an example on how to do the data block  */
   $query = "SELECT * FROM Fields ORDER BY date DESC";
   $result = /* code to process your MySQL query */;
   
   for ($i = 0; $i < mysql_num_rows($result); $i++)
   {
      $row = mysql_fetch_assoc($result);
      
      $blkItems[] = array(
                           'title' => '',
                           'link' => '',
                           'comments' => '',
                           'description' => '',
                           'guid' => '' . '@' . 'http://your.site.here',
                           'category' => '',
                           'pub_date' => date('r', )
                        );
   }
   
   $tbs->MergeBlock('blkItems', $blkItems, true);
   
   /* show output, but don't stop the script */
   $tbs->Show(TBS_OUTPUT);
?>

Please note that in my examples, I don’t include classes before I define them because I make use if PHP’s __autoload() function. Since you probably don’t, insert the necessary include statement(s) before any object declaration statements. Most of the variables are self-explanatory in this file.
Lines 12-23 define fields that are for the general feed. Lines 30-50 take care of the feed items. In my example, I run a MySQL query, and feed that data into a complex array. You can populate that array any way you want as long as you keep those element names the same and keep that MergeBlock function alone online 52.

<?php
   /* make sure that we are sending the correct header */
   header('Content-type: application/atom+xml');
   
   /* create template object */
   $tbs = new clsTinyButStrong();
   
   /* set main template */
   $tbs->LoadTemplate('atom.xml', false);
   
   /* define information fields */
   $info['lang'] = 'en-US';
   $info['charset'] = 'utf-8';
   $info['title'] = '';
   $info['description'] = '';
   $info['author_name'] = '';
   $info['link'] = '';
   $info['email'] = '';
   $info['tag'] = '';
   $info['pub_date'] = date('Y');
   $info['updated'] = date('c');
   $info['year'] = date('Y');
   $info['atom_link'] = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   
   
   /* for this section, you can include your entries through a query (if your column names 
      match up with the fields), or a data array */
   
   /* I'm going to use a data array from a MySQL table.  The following is an example on how to do the data block  */
   $query = "SELECT * FROM Fields ORDER BY date DESC";
   $result = /* code to process your MySQL query */;
   
   for ($i = 0; $i < mysql_num_rows($result); $i++)
   {
      $row = mysql_fetch_assoc($result);
      
      $blkItems[] = array(
                           'lang' => 'en-US',
                           'author' => 'Andrew Wells',
                           'title' => htmlspecialchars($row['title']),
                           'link' => PATH_WEB_HOME . 'fields/' . $row['slug'] . '/',
                           'comments' => PATH_WEB_HOME . 'fields/' . $row['slug'] . '/',
                           'summery' => $row['intro'],
                           'content' => $row['intro'],
                           'guid' => $row['field_id'] . '@' . PATH_WEB_HOME,
                           'category' => 'Fields',
                           'updated' => date('c', strtotime($row['updated'])),
                           'pub_date' => date('c', strtotime($row['added']))
                        );
   }
   
   $tbs->MergeBlock('blkItems', $blkItems, true);
   
   /* show output, but don't stop the script */
   $tbs->Show(TBS_OUTPUT);;
?>

Leave a Reply





Wordpress doesn't like it when you post PHP code. Go save your code at pastebin, and post the link here.

About the Author

Andrew has been coding PHP applications since 2006, and has plenty of experience with PHP, MySQL, and Apache. He prefers Ubuntu Linux on his desktop and has plenty of experience at managing CentOS web servers. He is the owner of Wells IT Solutions LLC, and develops PHP applications full time for anyone that needs it as well as does desktop computer support locally in the local area. He spends most of his free time exploring new programming concepts and posting on The Webmaster Forums.