开发者

Displaying RSS with PHP question

I'm trying to implement an RSS feed using PHP, as far as I can see it should be working, as if you view source of the page, you see the XML that should be output into the RSS feed (http://www.mattlewis.org.uk/web/rss.php)

Here's the code for the page:

    header("Content-Type: application/xml; charset=ISO-8859-1");
    include 'includes/connection.php';
    print '<?xml version="1.0" encoding="UTF-8"?>';
    print '<?xml-stylesheet type="text/xsl" href="rssXSLT.xsl"?>';
    print '<rss version="2.0">

    <channel>
    <title>FuseArt News</title>
    <description>Welcome to our RSS news feed!</description>';

 $query = "SELECT * FROM fuseArt_News";
 $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

 while ($row = mysql_fetch_array($result))
 {
  $_xml .="<news_item>";
   $_xml .="<title>" . $row['Title'] . "</t开发者_JS百科itle>";
   $_xml .="<date>" . $row['Date'] . "</date>";
   $_xml .="<article>" . $row['Article'] . "</article>";
  $_xml .="</news_item>";

  print $_xml;  
 }

 print '</channel>
 </rss>';

Anyone got any ideas as to why it may not be outputting?

Thanks


You've got the names of the required elements in an RSS document wrong. It should be <item> (there's no <news_item>), <description> (not <article>) and <pubDate> (not <date>). Also with dates, unfortunately, you have to use the old-school RFC822-family format (“Mon, 01 Jan 2000 00:00:00 GMT”) rather than the ISO-8601-style format you're using at the moment.

Also, your charsets don't match, and you need to call htmlspecialchars() (it's OK for XML too) over every string value you inject into the XML (like $row['Title']), otherwise stray < and & characters will completely break your feed. And if $news['Article'] is plain text rather than HTML markup, you will need to HTML-escape it again (double-htmlspecialchars(), once for text-to-HTML, once for HTML-to-HTML-in-XML).

Anyway, why all the $_xml? PHP is a templating language, you might as well use it for templating:

<?php
    function h($s) {
        echo htmlspecialchars($s, ENT_QUOTES, 'utf-8');
    }
    function redoDateFormat($s) {
        return gmstrftime('%a, %d %b %Y %H:%M:%S GMT', strtotime($s));
    }

    // check charset is right... should it be ISO-8859-1 or UTF-8?
    header('Content-Type: text/xml;charset=utf-8');
    $newses= mysql_query('SELECT * FROM fuseArt_News ORDER BY `Date` DESC');
?>
<rss version="2.0"><channel>
    <title>FuseArt News</title>
    <description>Welcome to our RSS news feed!</description>
    <?php while ($news= mysql_fetch_assoc($newses)) { ?>
        <item>
            <title><?php h($news['Title']); ?></title>
            <pubDate><?php h(redoDateFormat($news['Date'])); ?></pubDate>
            <description><?php h($news['Article']); ?></description>
        </item>
    <?php } ?>
</channel></rss>        


Is your document RSS2 compliant? I mean, those tags you've used are valid RSS2 tags? I suggest you to switch to this more common syntax: http://www.petefreitag.com/item/465.cfm

Let me know if it works. It always worked for me.


As Pekka has stated you want to make sure your charsets are the same, I'd recomend you use UTF-8 on both. I cannot see why this wont work providing the data in your database is there and correct.

One observation is that Id make sure that you put print $_xml; outside of the while loop or place $_xml =""; at the begining of the loop. Reason being is that the XMl keeps adding the the loop each time round and then you are print the whole contents over and over again making duplicates of your data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜