View Full Version : Can You Use PHP To Generate An XML File?
Pennimus
02-22-2006, 12:31 AM
The reason I ask is that I want to setup an RSS feed for my site. As the database integration and PHP code for displaying my blog posts is already in existence I thought it would be simplicity itself to slightly modify this code to create my feed.
Then I remembered that the feed would have to be an XML file and as such the PHP wouldn't parse properly.
What's the best way to handle this? I've no problem generating the feed with PHP from the database, but I'm stuck on how to get the resulting XML code into an actual XML file, or to get it to read properly as XML without a .xml extension.
missing-score
02-22-2006, 12:36 AM
For actually creating an XML file you can either use simple text methods or if you have PHP 5, use the DOM (http://www.php.net/dom).
To keep the XML data with a PHP extension, just use:
header( 'Content-Type: text/xml' );
and then just print out your XML after that... Should work fine :)
Pennimus
02-22-2006, 12:39 AM
So basically the actual file can be rss.php, but as long as the header is set rss aggregators will read it properly as xml?
Man, it really is really simple! Thanks MS
missing-score
02-22-2006, 12:41 AM
So basically the actual file can be rss.php, but as long as the header is set rss aggregators will read it properly as xml?
Man, it really is really simple! Thanks MS
You should try to think of file extensions as nothing to do with whats in the actual file. I can serve my PHP pages with a .html extension. In fact, I could even serve them with a .asp extension if I wanted. Its content type that matters.
Pennimus
02-22-2006, 01:42 AM
Thanks for your continued help with this. It's working fine to a certain extent, but there is just one problem.
Here's my code :
<?php
header('Content-Type: text/xml');
$sql = mysql_connect ($host, $user, $pass) or die (mysql_error());
mysql_select_db ($data) or die (mysql_error());
$result = mysql_query ("SELECT title, id FROM blog ORDER BY id DESC LIMIT 5") or die(mysql_error());
?>
<rss version="2.0">
<channel>
<title>Board Crazy Skateboarding</title>
<link>http://www.board-crazy.co.uk/</link>
<description>Regularly updated with skateboarding news, articles and trick tips.</description>
<language>en</language>
<?php while ($item = mysql_fetch_array($result)){ ?>
<item>
<title><?php echo $item['title'];?></title>
<link>http://www.board-crazy.co.uk/blog.php?id=<?php echo $item['id'];?></link>
</item>
<? } ?>
</channel>
</rss>
The results of which can be found at www.board-crazy.co.uk/rss.php, and it validates okay as an RSS feed and looks right.
However you'll notice that I have not put in the xml declaration:
<?xml version="1.0"?>
That's because it was causing a parse error (I assume because it was thinking the <? part was starting some new php code).
I'm having a few problems with some feed readers like MyYahoo and MyMSN and I suppose it's possible this is caused by the lack of XML declaration.
How can I echo this out without php thinking it is php?! Can you escape php opening and closing tags?
missing-score
02-22-2006, 01:53 AM
you just need to echo it within a PHP string:
echo '<?xml version="1.0" encoding="iso-8859-1"?>';
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.