Thursday 14 March 2019

Creating Valid RSS feed in PHP

RSS Feature in content based website has been used by number of website owner around the world for get information of latest content information from online website.

RSS is a very simple broadcast feature which is used from introduce updated information to number of receivers.

RSS will automatically send changed content regularly to the user.

If you seen orange icon on many website, that means that website has provide content through RSS feed.

So, in this post we will make dynamic RSS feed by using PHP script from data has been fetch from Mysql database. In this post you can find out how to create your own custom RSS by using PHP script and Mysql. Before we will discuss which tag has been used to make RSS feed and what is use of that tag in RSS feed and lastly we will seen how to use PHP script for implementing dynamic RSS feed in PHP.

RSS feed must add following fields.

<?xml ?> - RSS feed must be start with this XML tag in which we have to define version of XML.

<rss> - Below XML tag we have start define RSS feed by using this tag, in which we can define version of RSS.

<channel> - In RSS feed first we have to create channel by using this tag.

<title> - RSS feed name has been define under this <title> tag.

<link> - RSS feed link has been define in <link> tag.

<description> - A short description of RSS feed must be set under this <description> tag.

<language> - We must have to define RSS feed language, which has been define under tag. For example, American English language RSS feed we have to write "en-us", here en is ISO-639 language code and us stand for ISO-3166 country codes.

All above tag has been in RSS feed header section, but all below tag will be used in body section of RSS feed which is under each item comes in the feed.

<item> - RSS feed item has been define by this tag.

<title> - RSS feed item title has been set under this tag.

<link> - Link of each item has been define under this <link> tag.

<description> - Under this tag we can define short description of each item, and description must contain at least 300 or more characters.

<pubDate> - Publication date of each item has been define under this <pubDate> tag and date must be ISO 8601 or RFC822 standard format.

<guid> - In RSS feed, we have to provide unique id for each item, which has been define under this <guid> tag.

- Name of the author has been define under this tag.
<enclosure> - Any media attachment like image or video link has been define under this tag, here we have also define size of attachment with it's mime type also.

Optional: <category> - This is optional tag in RSS feed and here we can define category of each item.

This is complete description of RSS feed tag, below you can find source code of How to create dynamic RSS feed in PHP with Mysql database.

Mysql Table

-- Table structure for table `tbl_post`

CREATE TABLE `tbl_post` (
  `id` mediumint(8) UNSIGNED NOT NULL,
  `post_title` text,
  `post_description` text,
  `author` varchar(255) DEFAULT NULL,
  `datetime` datetime DEFAULT NULL,
  `post_image` varchar(150) DEFAULT NULL,
  `post_url` varchar(150) DEFAULT NULL

-- Indexes for table `tbl_post`
ALTER TABLE `tbl_post`

-- AUTO_INCREMENT for dumped tables

-- AUTO_INCREMENT for table `tbl_post`
ALTER TABLE `tbl_post`




$connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");

$query = "SELECT * FROM tbl_post ORDER BY id DESC";

$statement = $connect->prepare($query);


$result = $statement->fetchAll();

header("Content-Type: text/xml;charset=iso-8859-1");

$base_url = "http://localhost/tutorial/how-to-create-dynamic-rss-feed-in-php/";

echo "<?xml version='1.0' encoding='UTF-8' ?>" . PHP_EOL;
echo "<rss version='2.0'>".PHP_EOL;
echo "<channel>".PHP_EOL;
echo "<title>Feed Title | RSS</title>".PHP_EOL;
echo "<link>".$base_url."index.php</link>".PHP_EOL;
echo "<description>Cloud RSS</description>".PHP_EOL;
echo "<language>en-us</language>".PHP_EOL;

foreach($result as $row)
 $publish_Date = date("D, d M Y H:i:s T", strtotime($row["datetime"]));
 $image_size_array = get_headers($base_url . "images/".$row["post_image"], 1);
 $image_size = $image_size_array["Content-Length"];
 $image_mime_array = getimagesize($base_url . "images/".$row["post_image"]);
 $image_mime = $image_mime_array["mime"];
 echo "<item xmlns:dc='ns:1'>".PHP_EOL;
 echo "<title>".$row["post_title"]."</title>".PHP_EOL;
 echo "<link>".$base_url."blog/".$row["post_url"]."/</link>".PHP_EOL;
 echo "<guid>".md5($row["id"])."</guid>".PHP_EOL;
 echo "<pubDate>".$publish_Date."</pubDate>".PHP_EOL;
 echo "<dc:creator>".$row["author"]."</dc:creator>".PHP_EOL;
 echo "<description><![CDATA[".substr($row["post_description"], 0, 300) ."]]></description>".PHP_EOL;
 echo "<enclosure url='images/".$row["post_image"]."' length='".$image_size."' type='".$image_mime."' />".PHP_EOL;
 echo "<category>PHP tutorial</category>".PHP_EOL;
 echo "</item>".PHP_EOL;

echo '</channel>'.PHP_EOL;
echo '</rss>'.PHP_EOL;


Reading RSS Feed with PHP

Above we have seen how to build RSS Feed using Dynamic Mysql data in PHP. Here you can find Reading of XML RSS Feed data using PHP script. There are many website which has use XML RSS feed for received data from other site for display on their website. There is one benifits of using XML RSS feed data is that content receiver site has not store other website data on their website, and benifits to content website owner is that he can publish their website content on other website by add content into RSS feed.

So, there benifits to both content publisher and content provider. This is main benifits of RSS feed. Below you can find complete PHP Source code of reading of RSS feed in PHP.



$feed_url = "http://localhost/tutorial/how-to-create-dynamic-rss-feed-in-php/index.php";

$object = new DOMDocument();


$content = $object->getElementsByTagName("item");


<!DOCTYPE html>
  <title>How to Read RSS Feed in PHP</title>
  <link rel="stylesheet" href="" />
  <script src=""></script>
        <script src=""></script>
  <div class="container">
   <br />
   <h2 align="center">How to Read RSS Feed in PHP</h2>
   <br />
   foreach($content as $row)
    echo '<h3 class="text-info">' . $row->getElementsByTagName("title")->item(0)->nodeValue . '</h3>';
    echo '<hr />';
    echo '
    <div class="row">
     <div class="col-md-3">
      <br />
      <img src="'.$row->getElementsByTagName("enclosure")->item(0)->attributes["url"]->nodeValue.'" class="img-responsive img-thumbnail" />
     <div class="col-md-9">
      <p align="right"><b><i>By '.$row->getElementsByTagNameNS("ns:1", "*")->item(0)->nodeValue.'</i></b></p>
      <br />
    echo '<br />';
    echo '<span class="label label-primary">'.$row->getElementsByTagName("category")->item(0)->nodeValue.'</span>';
    echo '<br />';
    echo '<hr />';
    echo '<br />';


1 comment: