Views :- 1524

How to create rss feed using PHP Codeigniter

Lets start with step by step guide to create RSS feed using PHP Codeigniter framework.In one of my article I had covered What is RSS and How it works?, First go through this article, you will get the fair idea about RSS feed and how it is useful.

Step #1:- Create trn_feed_article table This table contains rss feeds which you want to syndicate using php.

CREATE TABLE  trn_feed_article (
  'article_id' int(10) unsigned NOT NULL auto_increment,
  'article_name' varchar(100) NOT NULL,
  'article_url' varchar(100) NOT NULL,
  'article_descr' varchar(500) NOT NULL  
  PRIMARY KEY  (article_id)
);

Step #2:- Create Feed Controller As you know feed is nothing but your content representation in form of xml , which is defined in univeral format of rss.

Fetch the rss feeds from database $query = $this->MiscellaneousModel->getFeedArticles();. Set the content type rss+xml header("Content-Type: application/rss+xml");.

<?php
class Feed extends Controller {

	function Feed(){
		parent::Controller();
		$this->load->model('FeedModel');	
		$this->load->helper('xml');	
	}
	
	function index(){
	        $data['encoding'] = 'utf-8';
	        $data['feed_name'] = 'www.technicalkeeda.com';
	        $data['feed_url'] = 'http://www.technicalkeeda.com';
	        $data['page_description'] = 'Welcome to www.technicalkeeda.com feed url page';
	        $data['page_language'] = 'en-ca';
	        $data['creator_email'] = 'yashwantchavan@gmail.com';
	        $query = $this->FeedModel->getFeedArticles();  
	        $data['ARTICLE_DETAILS'] = null;
		if($query){
			$data['ARTICLE_DETAILS'] =  $query;
		}  
	        header("Content-Type: application/rss+xml");
	        $this->load->view('rss', $data);
    }

}
?>

Step #3:-Create rss.php as a view page Here we are going to create the xml file dynamically with our feed contents.Each item tag reprsent the feed item with link, title ad description.

<?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
	if(is_array($ARTICLE_DETAILS) && count($ARTICLE_DETAILS) ) {					
		foreach($ARTICLE_DETAILS as $loop){ 
  ?>    
        <item>
          <title><?php echo xml_convert($loop->ARTICLE_NAME); ?></title>
          <link><?php echo site_url('' . $loop->ARTICLE_URL) ?></link>
          <guid><?php echo site_url('' . $loop->ARTICLE_URL) ?></guid>
          <description><?php echo xml_convert($loop->ARTICLE_DESCR); ?></description>      
        </item>        
   <?php } } ?>    
    </channel>
</rss>

Step #4:- Query to trn_feed_article table Fetch Your feed articles from the database define the method in Feedmodel class

function getFeedArticles(){	
		$this->db->select("ARTICLE_ID,ARTICLE_NAME,ARTICLE_DESC,ARTICLE_URL,ARTICLE_DESCR");
		$whereCondition = $array = array('ACTIVE_STATUS' =>'A');				 
		$this->db->where($whereCondition); 	
		$this->db->order_by("ARTICLE_ID", "desc");
		$this->db->from('trn_feed_article');				
		$query = $this->db->get();		
		return $query->result();			
	}

Step #5:- Finish :)

http://www.technicalkeeda.com/feed

php-codeigniter  rss  rss-feed 

Current Donation 10 $

You also Like this Tutorials

HTML Comment Box is loading comments...

Other Posts

 
Hi I am Yashwant founder of www.technicalkeeda.com, Purpose of this website to share the programming knowledge in the form post , blogs and articles.
 

| Rss Feed | Contact Us | Find us on Google+