Building an RSS Feed in Code Igniter

I've had a few requests asking how I built my RSS feed since I'm using a custom built Code Igniter blogging system. Actually, it was pretty straight forward, but I thought I'd take a few moments to outline step by step how I did it.

Most of the inspiration (and a lot of blatant stealing) is taken from this post about RSS on the Code Igniter forums. Please note, that this requires that you load the URL helper... which I autoload, but could just as easily be included in the contoller (thanks Attos). I start out by creating a controller named "feed".

class Feed extends Controller 

function Feed()
function index()
$data['page_description''Code Igniter, PHP, and the World of Web Design';
$data['creator_email''Derek Allard is at derek at derekallard dot com';
header("Content-Type: application/rss+xml");

An important thing to notice about the feed controller is that it sends a content-type along with it that indicates its a news feed - "application/rss+xml". Without that, browsers will try to display the file raw, or parse it as html. Both of those is not what we want. You'll also notice that it references a a model function called "getRecentPosts()". Here's that function inside the posts_model.

function getRecentPosts ()

You'll notice reference to 'post_visible' in there. I just use that as a flag for whether or not I want a particular post "live". This gives me the ability to save and create drafts, without them being visible to the world. The controller also loads a view called 'rss'. Here's the view.

echo '<?xml version="1.0" encoding="utf-8"?>' "\n";
<rss version="2.0"

title><?php echo $feed_name?></title>

link><?php echo $feed_url?></link>
description><?php echo $page_description?></description>
dc:language><?php echo $page_language?></dc:language>
dc:creator><?php echo $creator_email?></dc:creator>

dc:rights>Copyright <?php echo gmdate("Y"time()); ?></dc:rights>
admin:generatorAgent rdf:resource="" />

<?php foreach($posts->result() as $entry): ?>

title><?php echo xml_convert($entry->post_title); ?></title>
link><?php echo site_url('blog/post/' $entry->url_title?></link>
guid><?php echo site_url('blog/post/' $entry->url_title?></guid>

str_replace('/img/post_resources/'base_url() . 'img/post_resources/'$entry->post_body); ?>
pubDate><?php echo date ('r'$entry->post_date);?></pubDate>

<?php endforeach; ?>

The view uses PHP to echo out the opening <?xml version="1.0"?> element. That's because I was having trouble with the server interpretting the opening <? as PHP. It's a pretty common problem actually. Finally, this line is noteworthy.

<?str_replace('/img/post_resources/'base_url() . 'img/post_resources/'$entry->post_body); ?> 

I reference images and other resources absolutely in the code, but for the feed that needs to be changed to a http:// path reference, so I'm just using using the PHP str_replace() function to rewrite the paths of the images in the feed.


freaksauce wrote on

Fantastic little tutorial Derek, you always make it seem so effortless! 

Yannick wrote on

Nice and easy to follow tutorial Derek. Thanks for sharing how you implemented it.

Derek wrote on

Thank you both!  I can’t seem to surf the web without RSS anymore, so getting that up and running was a priority.

thegenerator wrote on

Thanks again for the nice work Derek

Shahrier Akram wrote on

Thank you so much Derek!

einchi wrote on

Nice thanks^^