How to Create RSS Feed For Website using PHP

Nowadays, most website owners are sharing their site content using social media. So, many Facebook, Twitter, Google plus or other social media users are using the stream of their favorite social network to remain aware of website changes. Nevertheless, RSS remains the first choice for content syndication. You may also like How to create RSS Feed reader using PHP

In this tutorial we will show you how to create RSS Feed for dynamic website using PHP.

PHP Code

<?php

define('DB_SERVER', "localhost");
define('DB_USER', "root");
define('DB_PASS', "");
define('DB_DATABASE', "hd_blog");
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);

$sql = "SELECT * FROM tbl_add_post ORDER BY id ";
$query = mysqli_query($con, $sql);

header("Content-type: text/xml");

echo "<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0'>
<channel>
<title>scripts.guru Programming Blog</title>
<link>https://scripts.guru</link>
<description>Programming Blog</description>
<language>en-us</language>";

while ($row = mysqli_fetch_assoc($query)) {
    $title = $row['title'];
    $link = $row['slug'];
    $description = $row['meta_description'];

    echo "<item>
<title>$title</title>
<link>$link</link>
<description>$description</description>
</item>";
}
echo "</channel></rss>";
?>

Leave a Reply

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