Monday, March 1, 2010

Create xml rss feed with PHP

First, write a script like below;

<?php header('Content-type: text/xml'); ?>

<?php $phpver = phpversion(); ?>

<rss version="2.0">
<channel>
<title>SapuraTIE</title>
<description>Sapuratie</description>
<language>en-US</language>
<link>http://thegreatestpost.blogspot.com/</link>
<copyright>Your copyright</copyright>
<generator>
XML/RSS feed generator by thegreatestpost(PHP version
<?php echo $phpver; ?>)
</generator>

<managingEditor>Site Editor</managingEditor>
<webMaster>Webmaster Email</webMaster>



<?php
require_once('db_connect.php'); //your connection to database

$query = "SELECT id,content_title,content_data,content_date,
content_page FROM sapurastie_article
WHERE content_section <> 10 AND
content_language =
$lang ORDER BY content_date DESC LIMIT 0,15"; //your query
$execute = mysql_query($query) or die(mysql_error());

while($result = mysql_fetch_array($execute)) {
?>
//Start create item
<item>
<title> <?=htmlentities(strip_tags($result['content_title'])); ?></title>
<description>
<?=htmlentities(strip_tags(substr($result['content_data'],0,500)."...",'ENT_QUOTES'));
?>

</description>

<pubDate>
<?php
echo date('r', strtotime($result['content_date'])); ?>
</pubDate>
<guid>http://thegreatestpost.blogspot.com</guid>
</item>
<?php } ?>

</channel>
</rss>

then, the output will be like;



Created by: thegreatestpost.



No comments: