ad

Generate RSS Feed With PHP

rss_icons

RSS feed lets your website visitors subscribe to get latest updates. If you are using WordPress or any other kind of CMS, you do not need this. Because all popular CMS comes with RSS feed. But this is not the case if you are developing your own custom coded website. For your website, you can easily create RSS feed. You only need to understand the structuer of RSS feed.

Sample RSS feed.

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

<title>Your Website Title</title> 
<link>Website URL</link> 
<description>Website description</description> 
<language>en-us</language>

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

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

</channel> 
</rss>

You only need to follow this structure. You can add as many Items in RSS as you want. Fetch article title, link of the article, and short description. If you want to show full description, you can add full description.

<?php
include('db.php'); // add database connection in db.php file and include it.

$sql = "SELECT * FROM website_posts ORDER BY id DESC LIMIT 20"; 
$query = mysql_query($sql);

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

echo "<?xml version='1.0' encoding='UTF-8'?> 
<rss version='2.0'>
<channel>
<title>Webtips - Web Design Blog</title>
<link>https://www.usethistip.com</link>
<description>WordPress, jQuery, CSS, HTML5 and web design blog</description>
<language>en-us</language>";

while($row = mysql_fetch_array($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>"; 
?>

 

Tags: |
Nord VPN Sale

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


Similar Articles

1 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip Developed By Deepanker