Create RSS Feed In Site

What is RSS Feed ?

RSS is the short name of Really Simple Syndication.RSS is like a broadcast system which is used to deliver updated information to multiple receivers.RSS is used to deliver regular changing web contents to the user.

In many sites, we see an icon like this ,That means, RSS Feed Is available on Site.

rss-icon

XML : RSS feed basic structure

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

<title>Title of Webpage</title>
<link>Webpage URL</link>
<description>About Webpage</description>
<language>en-us</language>

<item>
  <title>Article Title</title>
  <link>Article URL</link>
  <description>Article Content</description>
</item>

</channel>
</rss>

SQL : Creating the MySQL RSS Table

CREATE TABLE rss_info
 (
 id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
 title VARCHAR(200),
 link VARCHAR(200),
 description TEXT
 );

Creating a valid RSS 2.0 feed with PHP

If the Feed link will be something like this: http://www.example.com/rss.php

<?php
// Create connection
 $con=mysqli_connect("example.com","alex","qwerty");
// Check connection
 if (mysqli_connect_errno($con)) {
 echo "Database connection failed!: " . mysqli_connect_error();
 }
 
 $sql = "SELECT * FROM rss_info ORDER BY id DESC LIMIT 20";
 $query = mysqli_query($con,$sql);
 
 header( "Content-type: text/xml");
 
 echo "<?xml version='1.0' encoding='UTF-8'?>
 <rss version='2.0'>
 <channel>
 <title>w3schools.in | RSS</title>
 <link>/</link>
 <description>Cloud RSS</description>
 <language>en-us</language>";
 
 while($row = mysqli_fetch_array($con,$query)){
   $title=$row["title"];
   $link=$row["link"];
   $description=$row["description"];
 
   echo "<item>
   <title>$title</title>
   <link>$link</link>
   <description>$description</description>
   </item>";
 }
 echo "</channel></rss>";
?>

Reading RSS with PHP

<?php
 $domOBJ = new DOMDocument();
 $domOBJ->load("rss.xml");//XML page URL
 
 $content = $domOBJ- >getElementsByTagName("item");
 
 foreach( $content as $data )
 {
   $title = $data->getElementsByTagName("title")->item(0)->nodeValue;
   $link = $data->getElementsByTagName("link")->item(0)->nodeValue;
   echo "$title :: $link";
 }
?>


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *