Welcome to WebmasterWorld Guest from 54.145.209.34

Forum Moderators: bill & werty

php, mysql & rss

How do you use php to create RSS?

   
11:42 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



Just starting in RSS and want to fill the RSS file using mysql & php.

I'm starting the rss file with:
<?xml version='1.0'?>
<rss version='2.0'>

And then the channel info, then the php to create the item info (pulled from mysql). Then closing the channel & rss.

This validates, but when viewed in a browser, the php info is visible rather than the items. When added to an aggragator (my yahoo), only the channel info is visible. Any ideas on what I'm doing wrong? Thanks!

12:20 am on Dec 8, 2004 (gmt 0)

10+ Year Member



does it have a .php ending?
If you call it feed.xml or feed.rss it's not going to be parsed by php.

Also, when it is parsed by php it's going to choke on the <?xml . so use echo '<?xml ..?>' in php.

Finally, you will need to add a xml content type header.
e.g.
header('Content-Type: text/xml');

12:35 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Administrator mack is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



You may need to do a little work to make your server handle the php as "php" becase your file is rss you might need to alter your mime type to let php work within an .rss extension.

Mack.

12:51 am on Dec 8, 2004 (gmt 0)

10+ Year Member



You may need to do a little work to make your server handle the php as "php" becase your file is rss you might need to alter your mime type to let php work within an .rss extension.

E.g. Putting this in an .htaccess file in the same folder as "feed.xml" would force the webserver to parse the file as php

<Files feed.xml>
ForceType application/x-httpd-php
</Files>

4:22 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Thanks, the echo '<?xml ..?>' and content type header is helping, and I'll let you know how it goes getting it parsed as php.

Appreciate the help.

4:14 pm on Dec 24, 2004 (gmt 0)

10+ Year Member



Just got back to this project, and still having troubles. Added the .htaccess file to the folder with the xml file, and using echo for the xml. The URL is <snip> Here's the code:


header('Content-Type: text/xml');

echo '<?xml version='1.0'?>'
echo '<rss version='2.0'>'

echo '<channel>'
echo '<title>Example.com Test RSS 2</title>'
echo '<description>Anytown Widgets and more! Click for The County's Internet Marketplace.</description>'
echo '<link>http://www.example.com/</link>'
echo '<copyright>Copyright 1998 - 2004 example.com. All Rights Reserved</copyright>'

<?php
include("../admin/config.php");

$sql = "SELECT category, adtext, id FROM classifieds WHERE postdate >= NOW() - INTERVAL 2 DAY AND status = 'a' ORDER BY RAND() DESC LIMIT 3";

$result = mysql_query($sql, $conn) or die (mysql_error());

//GO THROUGH EACH ROW IN THE RESULT AND DISPLAY DATA

while ($newArray = mysql_fetch_array($result)) {
//give a name to the fields
$category = $newArray['category'];
$adtext = $newArray['adtext'];
$id = $newArray['id'];

//WRITE ITEM INFO

fwrite ($fp, "<title>$category</title>");
fwrite ($fp, "<description>$adtext</description>");
fwrite ($fp, "<link>$id</link>");
fwrite ($fp, "<item>$item</item>");

}

?>

echo '</channel>'
echo '</rss>'

The error I'm getting is "invalid at the top level of the document.

[edited by: werty at 8:48 am (utc) on Dec. 26, 2004]
[edit reason] Removed URL, Widgitized the description, changed domain to example.com. [/edit]

5:16 pm on Jan 6, 2005 (gmt 0)

10+ Year Member



Try moving your "<?php" and "?>" to the first and last lines of your program.
As it stands you're trying to execute PHP commands ( echo, header ) outside of where you are declaring that it is PHP code - your webserver is probably treating them as html, not as PHP commands.

Hope this works for you

John

 

Featured Threads

Hot Threads This Week

Hot Threads This Month