PHP RSS feed generator form to entering data to a table

If you are a web publisher or maintain web sites then it is a good idea to give RSS feed to your visitors or other webmasters to publish your contents in their websites or in other Medias. RSS is quite popular in the field of content syndicating and going to play a major role in days to come.

By using RSS feed in your web site you can tell about your site updates and new articles / pages to the world without much effort. Here we will discuss on creating a RSS feed by using PHP script. Here we will focus on developing XML format for RSS feed. There are many ways to do this and here we will discuss one of the way which this site uses.

There are two steps involved in developing a RSS feed. First we will update a table with new updates and second step is updating the RSS page from the table. You can bypass the table and directly update the RSS page but by using a table we can keep a history of previous updates and its date of publications.

The first step is creating a form and then storing the entered data to a table. Here the form is designed to take data as per the required format of RSS feed. We need four inputs, one is title (this will be used as anchored text so important for our keyword ranking), Link (the URL of the page), description and publication date. The publication date has to be in a particular format known as RFC-822 date format. The format looks like this

<pubDate>Sat, 11 Oct 2004 09:00:00 EST</pubDate>
<pubDate>Mon, 05 Sep 2002 14:00:00 GMT</pubDate>
<pubDate>Wed, 14 Oct 2003 17:00:00 +0200</pubDate>
So we will pre populate the date field with present time in this format, but we will have option of changing it. Here is the sample form.
echo "<hr>";
$tm=time();  // time stamp of the present time
$tm=date("D, d M Y H:i:s",$tm); // generating the format
$tm=$tm. " GMT"; // added GMT but you can add your format
echo "<form method=post action=rsspostck.php>
Title<input type=text name=title size=100><br>
Link<input type=text name=page_link size=100><br>
Description<textarea name=description cols=60 rows=5></textarea><br>
Publication Date<input type=text name=pubdate size=35 value='$tm'>Wed, 21 Apr 2005 08:20:47 GMT<br>
<input type=submit value='Add'></form>
";
We will go with a simple date field for storing the RSS feeds, but we will have option of changing it.

You can download the zip file with all the scripts to create your own RSS feed or to understand how the script works at the end of this page. Inside this zip file these are the files available.

config.php

Store all database connection details and this page is included wherever database operation is involved.

rsspost.php

This page display the form for the user to enter page details. This page post details to rsspostck.php where all the data is inserted to our rss table.

rsspostck.php

Collects all the form data from rsspost.php file and then stores them in a table.
require 'config.php';
$title=$_POST['title'];
$link=$_POST['link'];
$description=$_POST['description'];
$pubdate=$_POST['pubdate'];

echo "<hr>";

$sql=$dbo->prepare("insert into rss(title,link,description,pubdate) values(:title,:link,:description,:pubdate)");
$sql->bindParam(':title',$title,PDO::PARAM_STR, 250);
$sql->bindParam(':link',$link,PDO::PARAM_STR, 250);
$sql->bindParam(':description',$description,PDO::PARAM_STR,250);
$sql->bindParam(':pubdate',$pubdate,PDO::PARAM_STR);
if($sql->execute()){
$rss_id=$dbo->lastInsertId();
echo " Thanks .. Your Rss Id = $rss_id ";
}
else{
echo " Not able to add data please contact Admin ";
print_r($sql->errorInfo());
}

update_rss.php

Here we will read the recent data from the table and update the xml file in the specified format. Now we will read the data from the table and with the new data we will update the xml file. We will be using PHP file open command to open the xml file and then enter the new data to this file and then close the file. You can read the related tutorials here to get the idea on how to handle files in PHP. We will use limit command in SQL to get the most recently added 6 records from the table. Here we have used the order by command with desc to collect the records from the end of the table. Here is our SQL to collect the records from the table. Here is the code of it.
<?Php
require 'config.php';

///////// Getting the data from Mysql table for first list box//////////
$sql="SELECT * from rss order by rss_id desc limit 0,6"; 
///////////// End of query for first list box////////////

$body="<?xml version="1.0" encoding="iso-8859-1"?> 
<rss version="2.0"> 
<channel> 
	<title>Plus2net Free Tutorials on Web programming</title> 
	<link>https://www.plus2net.com</link> 
	<description>The latest Tutorials and free codes on Web programming from www.plus2net.com</description> 
	<copyright>(c) 2005, Plus2net.com. All rights reserved.</copyright> 
";

foreach ($dbo->query($sql) as $nt) {
$body .="
			<item>
				<title> $nt[title]</title>
				
		<link> $nt[link]</link> 
				<description> $nt[description]</description>				
				
				<pubDate>$nt[pubdate]</pubDate>
			</item>";
}

$body .="
</channel>
</rss>";
echo $body;
$path="rss.xml";
	$filenum=fopen($path,"w");
	fwrite($filenum,$body);
	fclose($filenum);

?>
After running of this update_rss.php file, one new rss.xml file will be generated. So you must give write permission to this directory to create this file.

Download the code for this tutorial here


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer