Recent changes RSS feed
 

This is a simple tutorial explaining how to utilise RSS/Atom feeds on your website using a combination of WACT and the Magpie RSS Parser.

First thing you need to do is download the Magpie source from http://magpierss.sourceforge.net/. Once you have this it can be integrated into WACT by simply extending the ArrayDataSet class as follows:

require 'rss_fetch.inc';
require '../../framework/common.inc.php';
require WACT_ROOT . '/util/arraydataset.inc.php';
require WACT_ROOT . '/template/template.inc.php';

class RSSFeed extends ArrayDataSet {
    var $rss;
    function RSSFeed($url="") {
        $this->rss = fetch_rss($url);
        if(is_array($this->rss->items)) {
            $this->setDataSet($this->rss->items);
        }
    }

    function getChannelData($item='') {
        return $this->rss->channel[$item];
    }
}

Then it is just a matter of using standard ListTag syntax to display the feed:

$Page =& new Template('/rss.html');

$Feed =& new RSSFeed("http://www.livejournal.com/users/static_object/data/atom");

$Page->set('channel_title', $Feed->getChannelData('title'));

$FeedList =& $Page->getChild('rsslist');
$FeedList->registerDataset($Feed);

$Page->display();

If you’d like to use the PHP date() function to manipulate the date formatting, then a filter can be added:

class RSSFilter {
    function doFilter(&$row) {
        $row['issued'] = date('l dS of F Y @ g:i A', strtotime(str_replace("T", " ", $row['issued'])));
    }
}

$Page =& new Template('/rss.html');

$Feed =& new RSSFeed("http://www.livejournal.com/users/static_object/data/atom");

$Page->set('channel_title', $Feed->getChannelData('title'));

$FeedList =& $Page->getChild('rsslist');
$FeedList->registerDataset($Feed);
$FeedList->registerFilter(new RSSFilter);

$Page->display();

And the template source:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <style type="text/css">
      <!--
        body {
            background-color:#FFFFFF;
            font:normal normal normal 11px/15px Verdana, Arial, Sans-serif;
            color:#000000;
            text-align:center;
        }
        #overall {
            width:770px;
            text-align:left;
            margin:0px auto 0px auto;
            /*border:1px dotted #ccc;*/
        }
        #masthead {
            border:1px dotted #ccc;
            padding:10px;
            background-color:#FFFFFF;
            font:normal normal normal 27px Garamond, Georgia, Serif;
        }
        #blog-contents {
            border:1px dotted #ccc;
            padding:5px;
        }
        .title {
            font:normal normal normal 18px Georgia, Garamond, Serif;
            margin:0px 0px 0px 0px;
        }
        .subtitle {
            font:normal normal normal 11px Arial, Verdana, Sans-serif;
            margin:4px 4px 8px 4px;
            color:#609933;
        }
        .postbox {
            padding:5px;
            border-bottom:1px dotted #ccc;
            margin-bottom:10px;
        }
      -->
    </style>
	<title>{$channel_title}</title>
  </head>
  <body>
    <div id="overall">
      <h1 id="masthead">{$channel_title}</h1>
      <div id="blog-contents">
        <list:list id='rsslist'>
          <list:item>
  		  <div class="postbox" id="{$id}">
              <h2 class='title'>{$title}</h2>
              <h4 class='subtitle'><a href="{$link}" title="Permalink"><img src="postlink.gif" width="8" height="9" alt="" border="0" /></a> Posted <b>{$issued}</b> by <b>{$author_name}</b></h4>
              <div class="post_contents">
                {$atom_content}
              </div>
            </div>
          </list:item>
        </list:list>
      </div>
    </div>
  </body>

</html>

Shown below is a list of the variable names used in both Atom and RSS feeds:

!Atom Feed fields: *title *id *link *issued *modified *author *author_name *atom_content

!RSS Feed fields: *guid *pubdate *link *description *comments *summary

If you’d like to see a demo of this in action, you can find one at http://www.flatnet.net/rss/

 
wact/parsing_rss_feeds_with_wact_and_magpie_rss.txt · Last modified: 2006/12/09 17:16
 
Hosting for this site donated by Procata PHP Development