Generating RSS feed

Posted by Vasilis on August 21, 2017

On this article we dicuss how to generate a rss feed outpur for the blog posts. We are going to make a simple output with the most basic information for the feed. This is the view file (core/views/rss.php)

<?php
header('Content-Type: application/rss+xml; charset=utf-8');
echo '<?xml version="1.0" encoding="utf-8"?>';
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title><?=$title?></title>
<link><?=$link?></link>
<description><?=$description?></description>
<atom:link href="<?=gila::config('base')?>rss" rel="self" type="application/rss+xml" />
<?php foreach($items as $item) { ?>
  <item>
  <title><?=$item->title?></title>
    <link><?=gila::config('base').'blog/'.$item->id?></link>
    <guid><?=gila::config('base').'blog/'.$item->slug?></guid>
    <pubDate><?=date('r',strtotime($item->updated))?></pubDate>
    <description><![CDATA[<?=$item->post?>]]></description>
  </item>
<?php } ?>
</channel>
</rss>
  • The date('r') is used to output the date in this format: Sun, 23 Jul 2017 06:14:51 GMT
  • permite us print html tags in description

 

Since its is a blog feed the controller to be used will be the blog class. We create the action feed:

    function feedAction()
    {
        $title=gila::config('title');
        $link=gila::config('base');
        $description=gila::config('slogan');
        $items=blog::latestposts();
        include 'src/core/views/rss.php';
    }

 The information is taken simply from the configuration variables. The function latestposts() returns a prefixed number of the latest published posts, in the form of generator of objects.

That's all it is, the path /blog/feed will return the rss feed. 

The feed of this website (more attributes have been added in the feed):

http://gilacms.com/blog/feed