Create RSS Feed with PHP

In this post we will see how to create RSS feed in PHP with MySQL.

RSS feed on website is one the best option of sharing your website content. In content Management System we have option of different plugin for this approach.

In PHP we will generate RSS feed for our site. First of all we must know about what is RSS feed.

What is RSS?

RSS stands for Rich Site Summary or also known as Really Simple Syndication. This is used to syndicating website content to all audience over the internet.

Feed is generated and user read it through some software application. RSS Feed is generated in XML format.


We will create a database table which store and retrieve data from it to generate RSS feed in XML format. Let us start to see process step by step:

Sample RSS feed format is shown below which we will generate with PHP using MySQL.

Sample RSS Feed Format

<?xml version='1.0' encoding='UTF-8'?> 
<rss version='2.0'> 
<channel>

<title>Website Title</title>
<link>Website Link</link>
<description>Website Description</description>
<language>en-us</language>
<copyright>Copyright (C) website.com</copyright>
        
<item>
    <title>Item or Article Title</title>
	<link>Item or Article Link</link>
    <description>Item or Article Description</description>
    <pubDate>Item or Article Publish Date</pubDate>
</item>
       
</channel>
</rss>

Above snippet is sample format of RSS feed in XML format.We will generate this RSS feed in PHP with MySQL.

MySQL

Create a MySQL table to store article title, link, description and publish date. This data will retrieve in PHP while loop which get formatted into RSS feed. Create a table named rss_feed.

CREATE TABLE IF NOT EXISTS `rss_feed` (
  `id` int(15) NOT NULL AUTO_INCREMENT,
  `title` varchar(225) NOT NULL,
  `link` varchar(225) NOT NULL,
  `description` mediumtext NOT NULL,
  `pubdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

PHP

We have seen on site that always feed is accessed without file extension. So just create a folder with name “feed” inside root directory of your project and a file with name “index.php” and copy the below snippet.

Now we can access website feed URL like this http://www.site.com/feed/

<?php
DEFINE ('DB_USER', 'dbuser');   
DEFINE ('DB_PASSWORD', 'dbpass');   
DEFINE ('DB_HOST', 'localhost');   
DEFINE ('DB_NAME', 'dbname'); 

mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
mysql_select_db(DB_NAME);

header("Content-type: text/xml; charset=utf-8");

$query = mysql_query("SELECT * FROM `rss_feed` ORDER BY `pubdate` DESC");

echo "<?xml version='1.0' encoding='UTF-8'?> 
<rss version='2.0'> 
<channel>

<title>PHPCluster</title>
<link>http://www.phpcluster.com</link>
<description>PHPCluster is a blog maintained by Vikash Kumar Singh.</description>
<language>en-us</language>
<copyright>Copyright (C) phpcluster.com</copyright>";

while($row = mysql_fetch_array($query))
{
extract($row);

echo "<item>
    <title>$title</title>
    <link>$link</link>
    <description>$description</description>
    <pubDate>$pubdate</pubDate>
    </item>";
}

echo "</channel>
      </rss>";

?>

Leave a Comment