Build RSS elements through an XML API so text is represented as XML nodes rather than manually concatenating markup from database values.
<?php
$rss = new SimpleXMLElement('<rss version="2.0"><channel/></rss>');
$channel = $rss->channel;
$channel->addChild('title', 'Latest tutorials');
$channel->addChild('link', 'https://www.plus2net.com/');
$channel->addChild('description', 'Recent Plus2net tutorials');
$item = $channel->addChild('item');
$item->addChild('title', 'PHP XML tutorial');
$item->addChild('link', 'https://www.plus2net.com/php_tutorial/php-xml.php');
$item->addChild('pubDate', gmdate(DATE_RSS));
echo $rss->asXML();
?>The examples below show additional XML structures, return values, and practical uses.
<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 = gmdate(DATE_RSS); // RFC 2822 / RSS date in GMT
echo "<form method=post action=rsspostck.php>
Title<input type=text name=title size=100><br>
Link<input type=text name=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.
require 'config.php';
$title = trim((string) (filter_input(INPUT_POST, 'title') ?? ''));
$link = filter_input(INPUT_POST, 'link', FILTER_VALIDATE_URL);
$description = trim((string) (filter_input(INPUT_POST, 'description') ?? ''));
$pubdate = trim((string) (filter_input(INPUT_POST, 'pubdate') ?? ''));
if ($title === '' || $link === false) {
exit('Enter a title and a valid URL.');
}
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());
}
<?php
require 'config.php';
$sql = 'SELECT title, link, description, pubdate FROM rss ORDER BY rss_id DESC LIMIT 6';
$doc = new DOMDocument('1.0', 'UTF-8');
$rss = $doc->createElement('rss');
$rss->setAttribute('version', '2.0');
$channel = $doc->createElement('channel');
$rss->appendChild($channel);
$doc->appendChild($rss);
foreach ($dbo->query($sql) as $row) {
$item = $doc->createElement('item');
foreach (['title', 'link', 'description', 'pubdate'] as $field) {
$tag = $field === 'pubdate' ? 'pubDate' : $field;
$node = $doc->createElement($tag);
$node->appendChild($doc->createTextNode((string) $row[$field]));
$item->appendChild($node);
}
$channel->appendChild($item);
}
$doc->formatOutput = true;
$doc->save('rss.xml');
echo $doc->saveXML();
?>
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
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.