Build Your Own RSS Feed with PHP and mySQL

Here’s a quick tutorial to build a RSS Feed based on your own database. Just replace the details and modify the SQL command. (I)

First we have to declare the header.. Remember, the header MUST be written in the first line of the script. this is why.

<?
header ("content-type: text/xml");

and then your database details

// Database config
$dbhost="localhost";
$dbname="your_db";
$dbuser="root";
$dbpass="rootpassword";
$koneksi = mysql_connect ($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
// database end

this is a must element of your RSS, just write it..

 $today = date("D, j M Y G:i:s");
 echo '<?xml version="1.0" encoding="UTF-8"?>';
 ?><rss version="2.0"
 xmlns:content="http://purl.org/rss/1.0/modules/content/"
 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/"
 >
 </rss>

you can tweak the details inside the element bellow:

<channel>
 <title>Android Repo Browser</title>
 <atom:link href="http://andiim3.com/feed" rel="self" type="application/rss+xml" />
 <link>http://andiim3.com</link>
 <description>Latest android AKP for download</description>
 <lastbuilddate>< ?=$today?> +0000</lastbuilddate>
 <sy:updateperiod>hourly</sy:updateperiod>
 <sy:updatefrequency>1</sy:updatefrequency>
 <generator>http://andiim3.com/feed</generator>
 </channel>

now we fetch your data from the database. Modify the SQL command based on your own database structure.

<?
 $sql=mysql_query("select * from apks order by `date` DESC LIMIT 20");
 while($data=mysql_fetch_array($sql)){
 $size=$data[size];
 if($size<=1024){$size=round($size, 2);$sizes="$size byte";}
 if($size>=1024){$size=$size/1024;$size=round($size, 2);$sizes="$size KB";} // KB
 if($size>=1024){$size=$size/1024;$size=round($size, 2);$sizes="$size MB";} // MB
 $date=$data[date];
 list($Y, $m, $d)=explode("-",$date);
$today = date("D, j M Y G:i:s", mktime(0, 0, 0, $m, $d, $Y)); //date("D, j M Y G:i:s");
 echo "<item>
 <title> $data[name]</title>
 <link>http://andiim3.com/?apk=$data[apkid]</link>
 <dc:creator>andiim3</dc:creator>
 <pubdate>$today +0000</pubdate>
 <guid isPermaLink=\"true\">http://andiim3.com/?apk=$data[apkid]</guid>
 <description>$data[name] (Ver: $data[ver]) - APK ID: $data[apkid] - Size : $sizes - Date added: $data[date]</description>
 <content:encoded>< ![CDATA[ <img src=\"$data[icon]\" />$data[name] (Ver: $data[ver]) - APK ID: $data[apkid] - Size: $sizes - Date added: $data[date]
 ]]></content:encoded>
 </item>
 ";}
 ?>

now close the feed

</channel>
 </rss>

See my feed here: andiim3.com/feed , and see the source (right click, view source)

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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.