Create RSS Feed in PHP

This is the easiest way to build a RSS feed in PHP. Because we don’t have to modify the feed file (feed.xml, or another xml file) anymore every we adding more content into the site.. And here’s the key concept:

Every the PHP file executed, the PHP file will output XML-based data to the browser by modifiying the PHP’s header.

In order to output a XML-based data by PHP, you will need to put this line into the PHP script:

header(‘Content-Type: text/xml; charset=UTF-8’, true);

the line above will send an xml file to your browser with XML format.

Now you will need to fill in the XML with RSS-formatted data, so the reader can read their RSS feed.

Below is the basic RSS format, you need some scripting basic to give the data into the XML.

<?xml version=”1.0″ encoding=”UTF-8″ ?>

<rss version=”2.0″
xmlns:content=”http://purl.org/rss/1.0/modules/content/”
xmlns:wfw=”http://wellformedweb.org/CommentAPI/”
xmlns:dc=”http://purl.org/dc/elements/1.1/”
xmlns:atom=”http://www.w3.org/2005/Atom”
xmlns:sy=”http://purl.org/rss/1.0/modules/syndication/”
>

<channel>
<title><? echo $sitename;?> RSS2 feed</title>
<atom:link href=”<? echo $sitepath;?>rss.php” rel=”self” type=”application/rss+xml” />
<link><? echo $sitepath;?>feed</link>
<description><? echo $desc;?></description>
<pubDate><?php echo mysql2date(‘D, d M Y H:i:s +0000’, $latest_post_date, false); ?></pubDate>
<generator>rss2</generator>
<language>en-us</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<?
$search = “SELECT * FROM $data_table order by id desc limit 10“;
$query = mysql_query($search);
while ($content = mysql_fetch_array($query))
{
?>
<item>
<title><?=$content[title]?></title>
<link><?=$sitepath.$content[url];?></link>
<comments><?$sitepath.$content[url].”#comm”; ?></comments>
<pubDate><?php echo mysql2date(‘D, d M Y H:i:s +0000’, $content[waktu], false); ?></pubDate>
<dc:creator><?=$content[user]?></dc:creator>
<guid isPermaLink=”false”><?=$sitepath.$content[url];?></guid>
<description><![CDATA[<?=$content[pembuka]; ?>]]></description>
<content:encoded><![CDATA[<?=$content[pembuka]; ?>]]></content:encoded>
<wfw:commentRss><?=$sitepath.”feed/comment/”;?></wfw:commentRss>
</item>
<? } ?>
</channel>
</rss>

The number of feed is depend on how many <item> tag that exist in the XML file.. You may find the bolded words “limit 10” in the line above, that will cause the PHP will only call the latest 10 data and put it into ten <item> tags.

If there are some error in parsing or reading, you may validate the RSS feed at this address: http://validator.w3.org/feed/

Andi Setiawan

Loving husband - Caring Father - Slap bet Commissionaire (I wish) find my complete profile on: http://andisetiawan.com/about-me http://facebook.com/andiim3 - http://gplus.to/andiim3 - http://andiim3.com

Share
Published by
Andi Setiawan

Recent Posts

Dedicated Blog for Dobby and Luna

Dobby and Luna have been part of our lives for the past years. They have…

2 years ago

Demo Site For My Projects

In the year 2021, I've completed some paid projects. Some of them are more technical,…

2 years ago

GlassTime – Glassmorphism WordPress Theme

Based on the GlassTime Bootstrap template, here I present glassmorphism WordPress Theme free to download.This…

3 years ago

GlassTime : Glassmorphism Responsive HTML5 Template

This HTML5 template is using Glassmorphism UI design language which is now trending for 2021…

3 years ago

Daftar Desa, Kecamatan, dan Kode Pos Buleleng – Bali

KecamatanKode POSBanjar81152Buleleng81119Busungbiu81154Gerokgak81155Kubutambahan81172Sawan81171Seririt81153Sukasada81161Tejakula81173Klik pada nama kecamatan untuk melihat daftar desa masing-masing. Sumber: kodepos.andiim3.com

3 years ago

Ninja diganti ADV

Ada yang bilang "semuanya akan mati matic pada akhirnya". Bener aja, sekarang tuntutan transportasi sehari-hari…

4 years ago

This website uses cookies.