为什么这个XML不能以我想要的方式在浏览器中呈现?

时间:2011-01-25 23:50:58

标签: php rss

我想用php从mysql创建RSS。我可以在页面源代码中看到内容。但我无法在Web浏览器(IE,Firefox或Opera)中看到该项目部分。 Web浏览器只显示

`您的RSS Feed名称或网站名称

您的Feed或网站的说明。

<?PHP
require_once ('mysql_connect.php'); 
    //SET XML HEADER
    header('Content-type: text/xml');

    //CONSTRUCT RSS FEED HEADERS
    $output = '<rss version="2.0">';
    $output .= '<channel>';
    $output .= '<title>Your RSS Feed Name or Website Name</title>';
    $output .= '<description>A description of your feed or site.</description>';
    $output .= '<link>http://www.yoursite.com/</link>';
    $output .= '<copyright>Your copyright details</copyright>';

    //BODY OF RSS FEED
    mysql_select_db("rss", $db);
    $result = mysql_query("SELECT * FROM rss limit 15",$db);
        while ($row = mysql_fetch_array($result)) {
            $output .= '<item>';
                $output .= '<title>'. $row['title'] .'</title>';
                $output .= '<description>'. $row['content'] .'</description>';
                $output .= '<link>'. $row['link'] .'</link>';
                $output .= '<pubDate></pubDate>';
            $output .= '</item> ';
        }
    mysql_close($db); 
    //CLOSE RSS FEED
    $output .= '</channel>';
    $output .= '</rss>';

    //SEND COMPLETE RSS FEED TO BROWSER
    echo($output);

?>

xml源看起来像:

<rss version="2.0">
<channel>
<title>Your RSS Feed Name or Website Name</title>
<description>A description of your feed or site.</description>
<link>http://www.yoursite.com/</link>
<copyright>Your copyright details</copyright>
  <item>
    <title>MILAN, ITALY - SEPTEMBER 26: Models walk the runway at Emi&hellip</title>       
    <description>Date: Sep 26, 2009 7:45 PMNumber of Comments on Photo:0View Photo&hellip;</description>
    <link>http://picasaweb.google.com/roxyluvtony/KendraSpears#5551895410815389042</link>
    <pubDate></pubDate>
  </item>
  ...
</channel>
</rss>

3 个答案:

答案 0 :(得分:1)

将我的评论移至答案

由于&hellip;<title>节点中的<description>实体,它不是有效的XML。

<![CDATA[tag]]>包裹这些字符串,然后重试。

答案 1 :(得分:0)

您正在获取常规数组而不是关联数组。它应该是:

while ($row = mysql_fetch_assoc($result))

区别在于mysql_fetch_array()将整数作为索引($row[0])获取,而mysql_fetch_assoc()会为您提供名称($row['title'])。

答案 2 :(得分:0)

可能是因为您的内容类型?试试Content-Type: application/rss+xml而不是Content-type: text/xml