homepage Welcome to WebmasterWorld Guest from 54.161.192.130
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / RSS, ATOM, and Related Technologies
Forum Library, Charter, Moderators: bill & werty

RSS, ATOM, and Related Technologies Forum

    
php, mysql & rss
How do you use php to create RSS?
Muon

5+ Year Member



 
Msg#: 127 posted 11:42 pm on Dec 7, 2004 (gmt 0)

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!

 

eaden

10+ Year Member



 
Msg#: 127 posted 12:20 am on Dec 8, 2004 (gmt 0)

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');

mack

WebmasterWorld Administrator mack us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 127 posted 12:35 am on Dec 8, 2004 (gmt 0)

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.

eaden

10+ Year Member



 
Msg#: 127 posted 12:51 am on Dec 8, 2004 (gmt 0)

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>

Muon

5+ Year Member



 
Msg#: 127 posted 4:22 pm on Dec 8, 2004 (gmt 0)

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.

Muon

5+ Year Member



 
Msg#: 127 posted 4:14 pm on Dec 24, 2004 (gmt 0)

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]

johnt

10+ Year Member



 
Msg#: 127 posted 5:16 pm on Jan 6, 2005 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / RSS, ATOM, and Related Technologies
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved