RSS feeds generator

RSS is an easy way to provide contents of your sites in form of XML. RSS is written using XML, so we can define our own tags while generating RSS feeds. Here I provided a sample PHP code how we can generate RSS feeds for the site. By following this blog you will be able to generate your own RSS like below using PHP and Mysql , here i used news related feeds, in real time you can define your own tags and content. Sample Output:
<?xml version='1.0' encoding='UTF-8' ?>
<rss version='2.0'>
<channel>
<title>Your Site Name</title>
<link>http://www.sforsuresh.in</link>
<description>Sample PHP RSS generation Code</description>
<item>
<title>RSS Tutorial</title>
<link>http://www.sforsuresh.in</link>
<description>Sample code to generate RSS using PHP</description>
<imagepath>http://www.sforsuresh.in/sample.jpg</imagepath>
<publishdate>2014-05-17 16:16:14</publishdate>
</item>
<item>
<title>RSS Tutorial</title>
<link>http://www.sforsuresh.in</link>
<description>Sample code to generate RSS using PHP</description>
<imagepath>http://www.sforsuresh.in/sample.jpg</imagepath>
<publishdate>2014-05-17 16:16:14</publishdate>
</item>
</channel>
</rss>
Below is the step by step process as how to generate the above. Let start with table. First we need a table which hold data for RSS. A sample structure for table is as below:
CREATE TABLE IF NOT EXISTS `news` (
`news_id` int(11) NOT NULL AUTO_INCREMENT,
`news_title` varchar(100) DEFAULT NULL,
`news` blob,
`image` varchar(100) DEFAULT NULL,
`publish_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`news_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
You can define or have a different table all together. I just taken a news table to create a news RSS feeds. Execute above statement in Mysql and dump some sample data. Now we need php code to generate RSS feeds. Create a file rssfeeds.php in your server and put the below code in that.
<?php
$mysqli = new mysqli("localhost", "root", "", "thenationline");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM news ORDER by news_id DESC";
echo '<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Your Site Name</title>
<link>http://www.sforsuresh.in</link>
<description>Sample PHP RSS generation Code</description>';
if ($result = $mysqli->query($query)) {
  while ($obj = $result->fetch_object()) {
    echo '
    <item>
    <title>'.$obj->news_title.'</title>
    <link>http://www.sforsuresh.in/'.$obj->news_id.'</link>
    <description>'.$obj->news.'</description>
    <imagepath>'.$obj->image.'</imagepath>
    <publishdate>'.$obj->publish_date.'</publishdate>
    </item>
    ';
}
$result->close();
}
echo "</channel>
</rss>";
$mysqli->close();
?>
That's all, just you need to access rssfeeds.php file from browser. [si-contact-form form='1']



Your feedbacks are most welcome..