Creating Dynamic RSS Feed Using PHP

Creating Dynamic RSS Feed Using PHP

How to make rss feed from mysql database, make rss using php, how to create php rss feed, download php rss feed script,

Creating an rss feed with PHP is very easy with few lines of codes you've done your first rss first script, rss (Rich Site Summary) is a format which used for generate rss feed from your web content, So it's very useful for your rss feeds user who don't want visit to check your content.


First creating table for RSS feed or you use your own database to generate rss feed from it.

CREATE TABLE IF NOT EXISTS `posts` (
  `post_id` int(11) NOT NULL,
  `post_title` varchar(266) NOT NULL,
  `post_description` varchar(255) NOT NULL,
  `post_link` varchar(255) NOT NULL,
  `post_catagory` varchar(255) NOT NULL,
  `post_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1;
ALTER TABLE `posts`
  ADD PRIMARY KEY (`post_id`);
Now here is our feed.php which contains the structure of xml and php query

<?php
//Connect to database
include 'connection.php';
//Check if user set limit for rss feed to show limited feed
if(isset($_GET['load']) === true || empty($_GET['load']) === false){
 $load = $_GET['load'];
}else{
//If not set then set it to 10,
 $load = 10;
} 
 //Get posts from database get it by limit
 $query = $pdo->prepare("SELECT * FROM `users` ORDER BY `post_id` LIMIT ?");
 //Bind the $load value to show feed 
 $query->bindValue(1,(int) trim($load), PDO::PARAM_INT);
 //Run the query
 $query->execute();
 
echo '<?xml version="1.0" encoding="UTF-8"?>';?>
&ltrss version="2.0">
<channel>
 <title>Merlaesson</title>
<description>Rss Feed</description>
 <link>http://www.meralesson.com</link>
 //Get all posts from database 
 <?php while($row = $query->fetch(PDO::FETCH_ASSOC)){?>
  <item>
    <title><?php echo $row['post_title']; ?></title>
     <description></description>
   <link><?php echo $row['post_link'];?></link>
    <pubDate><?php echo date("d/m/Y", strtotime($row['post_date']));?></pubDate>
     
<?php 
  } 
 ?>
</channel>
</rss>

Now we created the RSS feed, Let's change the feed.php to .RSS file extension so simply to your xampp/apache/conf and open httpd.conf file and search for AddType application and add new
AddType application/x-httpd-php .rss
below "AddType application/x-gzip .gz .tgz"

change the feed.php to .RSS file extension

now you can visit your feed.php as feed.rss 

Creating Dynamic RSS Feed Using PHP

You can also set limit for your feed by adding the code at end of your feed url example i want to see first 10 feed then i would add this www.mysite.com/feed.rss?load=10

Creating Dynamic RSS Feed Using PHP

Comments

  1. “Great share!” Each and every tips of your post are more informative and it was awesome to read.
    Thank you for such a great post…..

    ReplyDelete

Post a Comment