Tuesday, 14 November 2017

Build an RSS 2.0 Feed with CodeIgniter

CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(120) NOT NULL,
  `text` text NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM;
 
INSERT INTO `posts` (`id`, `title`, `text`, `date`) VALUES
(1, 'Some great article', 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using ''Content here, content here'', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for ''lorem ipsum'' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).', '2009-08-10'),
(2, 'Another great article', 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using ''Content here, content here'', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for ''lorem ipsum'' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).', '2009-08-10'),
(3, 'News from myfeed', 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using ''Content here, content here'', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for ''lorem ipsum'' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).', '2009-08-10');
 
Edit the autoload.php like so:

$autoload['libraries'] = array('database');
 
Edit the config.php like so:

$config['base_url'] = "http://localhost/YOUR DIRECTORY";
 
Edit the database.php like so:

$db['default']['hostname'] = "localhost"; // your host
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "tut_feeds";
 
Edit the routes.php like this:

$route['default_controller'] = "Feed";
 
In this controller, all the magic happens. Browse to system/application/controllers and create a new file
called feed.php. Next, create the Feed controller and have it extend the parent CI Controller.

    class Feed extends Controller {
       
      function Feed()
      {
        parent::Controller();
      }
}
 
Before the next step, we'll make use of CI's great helpers. Load the xml and text helper.

class Feed extends Controller {
       
      function Feed()
      {
        parent::Controller();
         
        $this->load->helper('xml');
        $this->load->helper('text');
      }


Next, will create a model to receive data from the database. If you don't know what models are, have a look at the CI
userguide. Browse to system/application/models
and create a file called posts_model.php.

class Posts_model extends Model {
     
    // get all postings
    function getPosts($limit = NULL)
    {
        return $this->db->get('posts', $limit);
    }
}
 
Now that we've created our model, we can continue with our feed controller. We'll load the posts_model that we just created.

class Feed extends Controller {
       
      function Feed()
      {
        parent::Controller();
         
        $this->load->helper('xml');
        $this->load->helper('text');
        $this->load->model('posts_model', 'posts');
      }
}
 
Now we create the index
method which is the method called by default. Let's set up some information for the feed view later too.

function index()
{
    $data['feed_name'] = 'MyWebsite.com'; // your website
    $data['encoding'] = 'utf-8'; // the encoding
    $data['feed_url'] = 'http://www.MyWebsite.com/feed'; // the url to your feed
    $data['page_description'] = 'What my site is about comes here'; // some description
    $data['page_language'] = 'en-en'; // the language
    $data['creator_email'] = 'mail@me.com'; // your email
    $data['posts'] = $this->posts->getPosts(10); 
    header("Content-Type: application/rss+xml"); // important!
}
 
Finally, we need to load the view which we will create in the next step.

function index()
{
    $data['feed_name'] = 'MyWebsite.com';
    $data['encoding'] = 'utf-8'; // the encoding
    $data['feed_url'] = 'http://www.MyWebsite.com/feed';
    $data['page_description'] = 'What my site is about comes here';
    $data['page_language'] = 'en-en';
    $data['creator_email'] = 'mail@me.com';
    $data['posts'] = $this->posts->getPosts(10); 
    header("Content-Type: application/rss+xml");
     
    $this->load->view('rss', $data);
}
 
Finally we have to create the view file - our output. Browse to system/application/views and crate a file called
rss.php.
First we set the xml version and the encoding within the head.
Followed by some rss meta information.
Now we will access the array $data from the previous step. We can access this data via the array keys, like so:

Now we need to loop, with foreach, to get all records.


<?php foreach($posts->result() as $post): ?>
 
   <item>
 
      <title><?php echo xml_convert($post->title); ?></title>
      <link><?php echo site_url('YOUR URL' . $post->id) ?></link>
      <guid><?php echo site_url('YOUR URL' . $post->id) ?></guid>
 
        <description><![CDATA[ <?php echo character_limiter($post->text, 200); ?> ]]></description>
        <pubDate><?php echo $post->date; ?></pubDate>
    </item>
 
     
<?php endforeach; ?>
 
    </channel>
<</rss>
 
 
 
 
 

0 comments:

Post a Comment