PHP RSS Feed

RSS technology is used by millions of users around the world to get the latest information from their favorite websites.

  • 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.
RSS icon rss-icon you see on some websites that mean, the RSS feed is available on site.

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

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

To get RSS content, you can use this script.

<?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";
 }
?>

Scroll Back to Top