首页 文章

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

提问于
浏览
2

我想用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

    将我的评论移至答案......

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

    <![CDATA[tag]]> 包裹这些字符串并再试一次 .

  • 1

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

    while ($row = mysql_fetch_assoc($result))

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

  • 0

    也许是因为你的内容类型?试试 Content-Type: application/rss+xml 而不是 Content-type: text/xml

相关问题