Get complete CodeIgniter blog project for free!

Downloaded more than 161 times, this is full project must-study for serious developers!

By submitting this form, you agree to our Terms of Service and Privacy Policy.

Create RSS Feed Using PHP and MySQL

This tutorial is about how to Create RSS Feed Using PHP and MySQL. RSS Feed is very significant for a website to deliver latest updates to users. User able to find website's latest updates on RSS Feed and they also can find previous posts on RSS Feed. You can get regular traffic by using RSS Feed. Feed Subscribers are your website's regular visitor. You will get a lot of returning visitor by this and If you will get this then your website will reach the top of search engine day by day. Google loves returning visitors, even every search engines like that. If you get a lot of returning visitor then it profs that you provide quality contents and these is helpful for users. Google loves the quality. You can do this easily if you use RSS Feed in your website. So why late? start using php atom feed reader from now. I will assist you to make this RSS Feed using PHP and MySQL. If you use dynamic RSS Feed then you will get a lot of facilities. Your RSS Feed Atom will update automatically if you update any content on your website. So now time to learn how to create RSS feed using MySQL and PHP. I am creating a live RSS Feed in this article. This is similar to Create XML Sitemap Using PHP and MySQL. just follow my instructions step by step.

Step-1. Create Database Table For RSSFeed


If you have a running dynamic website and wants to use RSS Feed to this website then just ignore this step. You have to choose a database and create a database table for RSS Feed item. We have created a table for RSS Feed example. Here is the table structure.

CREATE TABLE IF NOT EXISTS articles (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) DEFAULT NULL,
link VARCHAR(255) NULL,
date DATE DEFAULT NULL,
description TEXT DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;
Install import this table into your test database and follow the next step. Insert some data into this database table so that your RSS will be live with feed item. If you do this then our first step is complete now we have to go our next step.

Step-2. Create Necessary Functions For RSS Feed


In this step, we have to create some necessary functions. We have created this before. Now create a PHP file name as rss.class.php and paste this code into this file -

rss.class.php

<?php
class rss
{
public $conn;
public function __construct()
{
if(!$this->conn())
{
die('Failed to connect with MySQL');
self::close();
}
}

public function conn()
{
$host = "localhost";
$user = "root";
$pass = "";
$name = "test";
$conn = mysqli_connect($host,$user,$pass,$name);
if (mysqli_connect_errno())
{
die("Failed to connect with MySQL: ".mysqli_connect_error());
}
else
{
return $this->conn = $conn;
}
}
public function head($title,$description,$siteurl)
{
echo '<rss xmlns:atom="http://www.w3.org/2005/Atom"; version="2.0">';
echo '<channel>';
echo '<title>'.$title.'</title>';
echo '<link>'.$siteurl.'</link>';
echo '<description>'.$description.'</description>';
echo '<language>en-us</language>';
echo '<atom:link href="'.$siteurl.'/rss.xml" rel="self" type="application/rss+xml"/>';

}
public function feed($name,$url,$description,$publish)
{
echo '
<item>
<title>'.$name.'</title>
<link>'.$url.'</link>
<description>'.$description.'</description>
<pubDate>'.$publish.'</pubDate>
</item>
';
}
public function foot()
{
echo '</channel>';
echo '</rss>';
}
public function clean($string) {
$string = strtolower( preg_replace('@[\W_]+@', '-', $string) );
$string = rtrim($string,'-');
$string = strtolower($string);
return $string;
}

public function close()
{
mysqli_close(self::conn());
}
}

?>
These are our necessary functions to structured the RSS. First, you should configure MySQL to connect successfully with MySQL server. You can configure it from conn() function. Change $name, $pass, $user and other necessary information's you want to change. In head() function I defined XML RSS page configurations, RSS title, RSS description, RSS link. feed() function will help us to display RSS Feed item. We completed our second step, now we will go to the next step.

Step-3. Display RSS Feed in PHP


This step is for display RSS into PHP file. Just create a PHP file name as rss.php and write down this code bellow.

rss.php

 <?php
header("Content-Type: application/xml; charset=UTF-8");
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
date_default_timezone_set('UTC');
define('slash','/');
$url = 'http://'.$_SERVER['HTTP_HOST'];
include_once 'rss.class.php';
$rss = new rss;
$rssTitle = 'Latest Updates';
$rssDescription = 'Get Latest Updates Of This Site';
$rss->head($rssTitle,$rssDescription,$url);

$query = "SELECT * FROM articles ORDER BY id";
if ($result = mysqli_query($rss->conn(), $query)) {
while ($row = mysqli_fetch_assoc($result)) {
$link = $url.slash.rss::clean($row['name']).slash;
$date= date("D, d M Y H:i:s T", $row['date']);
if (preg_match('/^.{1,500}\b/s', $row['description'], $match))
{
$description=$match[0].'...';
}
rss::feed(htmlspecialchars($row['name']),$link,htmlspecialchars($description),$date);
}
mysqli_free_result($result);
}
$rss->foot();
$rss->close();
?>
This code should display full RSS Atom from the database table. If you install this completely following this steps then check it now. This will be displayed like http://example.com/rss.php. If you want to display it as http://example.com/rss.xml then you should rewrite some .htaccess rule. Append these code to your htaccess file. If you want to display RSS Feed RSS.xml then append following code

RewriteEngine On
RewriteRule ^rss.xml$ rss.php [NC,L]
If you want to display your RSS Feed like as http://example.com/feed/ then append this following code in your website's htaccess file.
 RewriteEngine On
RewriteRule ^feed/?$ rss.php [NC,L]
You will get some exclusive htaccess tips and tricks in 15 useful htaccess tips and tricks which were published in our blog before. If you do all task correctly then your job is complete. This is ready for use you can use this.

I hope you understand this. I attached full project with this article. You can download and start using instantly if you want. I hope this will be helpful for you. If you have any question or comment about this RSS Feed Using PHP and MySQL article please don't hesitate to do that, please comment your question in comment section.





Search on PHPAns
Subscribe to PHPAns
Join with our 32000+ subscribers and get our latest articles in your email inbox for free.
Delivered by FeedBurner
We always respect your privacy and take protecting it seriously.
Connect Social With PHPAns
Top