Skip to content

Build a RSS 2.0 feed with CodeIgniter?

RSS (Real Simple Syndication) feeds can be found everywhere on the internet. News and information are delivered using it and discussions forums, blogging sites enable usually different types of feeds for their users.

Whenever content defined by the RSS feed changes, feed offers to get an update. As an example, a blogging site could offer types “Full site feed”, “Comments-only feed”, “Label-specific site feed”, “Individual post comment feed”.

Here In this Tutorial, We will build RSS Feed With CodeIgniter Framework

I am assuming that you know basic installation of CI application, if no please have look on CodeIgniter Tutorials

Well, Then Create a CodeIgniter Controller (Class) in Controller folder (applications/controllers/feed.php) called feed.php,

load->helper(array('xml'));
    }
 
    public function index()
    {
	   // set feed Name will display at title area and page top
       $this->data['feed_name'] = 'phpis.com';
       // set page encoding
       $this->data['encoding'] = 'utf-8';
       // set feed url
       $this->data['feed_url'] = 'http://phpis.com/feed';
       // set page language 
       $this->data['page_language'] = 'en';
       // set page Description
       $this->data['page_description'] = 'PHP | CodeIgniter | WordPress | MySQL | Css3 | HTML5 | JQuery';
       // set author email 
       $this->data['creator_email'] = '[email protected]';
       // this line is very important, this will let browser to display XML format output
       header("Content-Type: application/rss+xml");
 
       $this->load->view('feed_view',$this->data);
 
    }
}
 
/*End of file feed.php*/
/*Location .application/controllers/feed.php*/

I have commented each and every line just go through the lines

Then its time to create view file , go to views folder it is located at controllers/views, Create a file called
feed_view.php,

' . "n";
?>


<?php echo $feed_name; ?>






Copyright 


 

replace with your data

replace with your data

replace with your data
 replace with your data 
replace with your data
 




Dynamic Rss Feed

We can easily Generate RSS feeds dynamically with very simple Coding.

Create a model Called rss_model.php in controller/model/.

db->get('posts');
        if ($query->numRows() > 0) { // check for rows
            return $query->result();
        }
        return;
    }
}
 
 
/*End of file feed_model.php*/
/*Location .application/models/feed_model.php*/

then load this model in our controller with following line

 $this->load->model('feed_model');

then call the model methods in controllers and pass it to views, see the complete controller


load->helper(array('xml'));
        // load the models
        $this->load->model('feed_model');
    }
 
    public function index()
    {
	   // set feed Name will display at title area and page top
       $this->data['feed_name'] = 'phpis.com';
       // set page encoding
       $this->data['encoding'] = 'utf-8';
       // set feed url
       $this->data['feed_url'] = 'http://phpis.com/feed';
       // set page language 
       $this->data['page_language'] = 'en';
       // set page Description
       $this->data['page_description'] = 'PHP | CodeIgniter | WordPress | MySQL | Css3 | HTML5 | JQuery';
       // set author email 
       $this->data['creator_email'] = '[email protected]';
       // get the feeds  from database through model method called get_feeds()
       $this->data['query'] = $this->feed_model->get_feeds();
       // this line is very important, this will let browser to display XML format output  
       header("Content-Type: application/rss+xml");
 
       $this->load->view('feed_view',$this->data);
 
    }
}
 
/*End of file feed.php*/
/*Location .application/controllers/feed.php*/

final step make the change in views file , open our view file called feed_view.php

' . "n";
?>


<?php echo $feed_name; ?>






Copyright 





<?php echo $row->post_title; ?>
post_url; ?>
post_id; ?>
post_description; ?> 

 




Thank so much !

0 0 votes
Article Rating
Subscribe
Notify of
guest

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments