Tag: PHP

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”.


<?php class Feed extends Controller { function Feed() { parent::Controller(); $this->load->model('posts_model', '', TRUE); $this->load->helper('xml'); } function index() { $data['encoding'] = 'utf-8'; $data['feed_name'] = 'DerekAllard.com'; $data['feed_url'] = 'http://www.derekallard.com'; $data['page_description'] = 'Code Igniter, PHP, and the World of Web Design'; $data['page_language'] = 'en-ca'; $data['creator_email'] = 'Derek Allard is at derek at derekallard dot com'; $data['posts'] = $this->posts_model->getRecentPosts(); header("Content-Type: application/rss+xml"); $this->load->view('feed/rss', $data); } } ?> ___________________________________________________________________

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 ()
        $this->db->orderby('post_date', 'desc');
        $this->db->where('post_visible', 1);
        return $this->db->get('posts');


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.

___________________________________________________________________ <?php echo '<?xml version="1.0" encoding="utf-8"?>' . "n"; ?> <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/"> <channel> <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="http://www.codeigniter.com/" /> <?php foreach($posts->result() as $entry): ?> <item> <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> <description><![CDATA[ <?= str_replace('/img/post_resources/', base_url() . 'img/post_resources/', $entry->post_body); ?> ]]></description> <pubDate><?php echo date ('r', $entry->post_date);?></pubDate> </item> <?php endforeach; ?> </channel></rss> ___________________________________________________________________ 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.
originally posted on : http://www.derekallard.com/blog/post/building-an-rss-feed-in-code-igniter/