dongpo4197 2010-11-11 12:04
浏览 30
已采纳

用PHP问题显示RSS

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'] . "</title>";
   $_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

  • 写回答

3条回答 默认 最新

  • dssu33392 2010-11-11 12:31
    关注

    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>        
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口