Hi forum,

Trying to create an RSS feed for my blog (not using a blog host or blog software). Seems to me the best way for me to do it is to use PHP in the XML to draw the last X posts from my MySQL database. So first, I added a .htaccess file to the directory with the line AddType application/x-httpd-php .xml ...to make the php work inside the xml file.

My rss.xml looks like:

<?php header('Content-type: text/xml'); ?>
<rss version="2.0">

<?php
(connection goes here)
(recordset goes here)
?>
<channel>
	<title>X</title>
	<description>I like the Internet</description>
	<link>http://www.x.com</link>

<?php
	mysql_select_db($aCon, $acCon);
    $top5q="SELECT blogIndex,title,content,DATE_FORMAT(date,'%a, %d %b %Y %T') AS rfcdate,tag1,tag2,tag3,tag4,tag5,tag6,tag7,tag8,tag9,tag10 FROM rhododendrites_blog ORDER BY date DESC LIMIT 10";
    $recentp = mysql_query($top5q,$acCon) or die(mysql_error());
	while($row=mysql_fetch_array($recentp))
		{ ?>
		<item>
			<title><?php
				echo htmlentities(strip_tags($row['title'])); ?>
			</title>
			<description>
				<?php echo $row['content']; ?>
			</description>
			<link>http://www.x.com/blog/post.php?blogid=<?php echo $row['blogIndex']; ?></link>
			<pubDate><?php echo $row['rfcdate']; ?></pubDate>
		</item>
		<?php } ?> 

</channel>
</rss>

I changed my website, of course, to x.com and the names of a couple vars. I'm new at this stuff and don't know what I should be paranoid about :) Sorry.

So not only does it not show any records (other than the initial channel info) when I surf to the URL of the feed directly, but when I try to validate it, I get this:

Sorry
This feed does not validate.
line 89, column 42: pubDate must be an RFC-822 date-time:
<pubDate><?php echo $row; ?></pubDate>

In addition, interoperability with the widest range of feed readers could be improved by implementing the following recommendations.
Feeds should not be served with the "application/x-httpd-php" media type

line 84, column 3: title should not be blank
</title>

line 90, column 2: item should contain a guid element
</item>

line 93, column 0: Missing atom:link with rel="self"
</channel>

Help? :/ Spent hours on this so far and can't figure it out.

looks like it is not parsing the php. maybe it's best to separate the php and xml. personally i use smarty templates to build the rss.

my rss code looks like this:

<?php
  require_once 'smarty/Smarty.class.php';
  $tpl = new Smarty();
  require_once './inc/defines.inc.php';
  $tpl->force_compile = false;
  $tpl->caching = 1;
  $tpl->cache_lifetime = 14400;     
  if ($tpl->is_cached('rss.tpl')) {
    $content = $tpl->fetch('rss.tpl');
  }
  else {
    $content = '';
    $db_link = @mysql_connect(HOST, USER, PASS);
    @mysql_select_db(DB);
    $query = "<omitted>";
    $result = @mysql_query($query);
    if ($result) {
      $rss = array ();
      while ($row = mysql_fetch_assoc($result)) {
        $dp = explode('-', $row['date']);
        $dt = mktime(12, 0, 0, $dp[1], $dp[2], $dp[0]);
        $row['date'] = date("D, d M Y H:i:s ", $dt) . '+0100';
        $row['article'] = substr($row['article'], 0, strpos($row['article'], '</p>'));
        $row['article'] = htmlentities(strip_tags($row['article']));
        $rss[] = $row;
      }
      $pd = date("D, d M Y H:i:s ", time()) . '+0100'; 
      $tpl->assign('PUBDATE', $pd);
      $tpl->assign('YEAR', date('Y'));
      $tpl->assign('RSS', $rss);
      $content = $tpl->fetch('rss.tpl');
    }
    if ($db_link)
      mysql_close($db_link);
  } 
  header("Content-Type: application/xml; charset=UTF-8");
  echo $content;
?>

and my template like this:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>Example - News</title>
    <link>http://www.example.com/page/news</link>
    <description>News</description>
    <language>en-us</language>
    <lastBuildDate>{$PUBDATE}</lastBuildDate>
    <pubDate>{$PUBDATE}</pubDate>
    <copyright>Copyright 2005-{$YEAR} Example</copyright>
    <image>
      <url>http://www.example.com/img/example.png</url>
      <link>http://www.example.com/</link>
    </image>
    {foreach from=$RSS item=item}
      <item>
        <title>{$item.title}</title>
        <link>{$item.url}</link>
        <description>{$item.article}</description>
        <author>{$item.author}</author>
        <pubDate>{$item.date}</pubDate>
        <category>{$item.category}</category>
      </item>
    {/foreach}
  </channel>
</rss>

you could use this.. its very simple..

<?php $db = new mysqli("localhost", "root", "", "rss"); ?>
<?php header('Content-type: text/xml'); ?>
<?php echo "<?";?>xml version="1.0" encoding="iso-8859-1"<?php echo "?>";?>
<rss version="2.0" xmlns:atom="http://www.geocities.com/jas_jo679">
<channel>
	<title>Making a Dynamic RSS Feed</title>
	<description>Getting items from the database.</description>
	<link>http://barbz.0fees.net/</link>
	<copyright>Your Copyright Information</copyright>
	<atom:link href="http://barbz.0fees.net/" rel="self" type="application/rss+xml" />
	<image>
        <url>http://www.upd.edu.ph/~music/graphics/up%20logo%20colored.gif</url>
    </image> 

	<?php
	$query = "SELECT * FROM `article`";
	$results = $db->query($query);
	$number = $results->num_rows;

	for ($i = 1; $i <= $number; $i++) {
		$row = $results->fetch_assoc();
		$title = $row['title'];
		$description = $row['body'];
		$link = $row['link'];
	?>
		<item>
			<title><?php echo $title; ?></title>
			<description><?php echo $description; ?></description>
			<link><?php echo $link; ?></link>
		</item>
		<?php
	}?>
</channel>
</rss>
<?php

$db->close();

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.