【问题标题】:Why does this XML not render in the browser the way I want?为什么这个 XML 不能以我想要的方式在浏览器中呈现?
【发布时间】:2011-01-25 23:50:57
【问题描述】:

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

`您的 RSS 提要名称或网站名称

您的供稿或网站的描述。`

<?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>

【问题讨论】:

  • "为什么浏览器没有按照我想要的方式呈现这个 PHP 的输出?"不如“为什么这个 PHP 输出这个 XML 而不是我想要的 XML?”这样一个好问题。或“为什么这个 XML 没有按照我想要的方式在浏览器中呈现?”
  • @jnpcl,像这样&lt;rss version="2.0"&gt;&lt;channel&gt;&lt;title&gt;Your RSS Feed Name or Website Name&lt;/title&gt;&lt;description&gt;A description of your feed or site.&lt;/description&gt;&lt;link&gt;http://www.yoursite.com/&lt;/link&gt;&lt;copyright&gt;Your copyright details&lt;/copyright&gt;&lt;item&gt;&lt;title&gt;MILAN, ITALY - SEPTEMBER 26: Models walk the runway at Emi&amp;hellip;&lt;/title&gt;&lt;description&gt;Date: Sep 26, 2009 7:45 PMNumber of Comments on Photo:0View Photo&amp;hellip;&lt;/description&gt;&lt;link&gt;http://picasaweb.google.com/roxyluvtony/KendraSpears#5551895410815389042&lt;/link&gt;&lt;pubDate&gt;&lt;/pubDate&gt;&lt;/item&gt;
  • @David Dorward,好的,我编辑了。
  • @yuli 您应该将 XML 编辑到问题中,这样很难阅读......它是否通过了 RSS 验证?您不应该发送 RSS 特定的内容类型吗?
  • 乍一看,它不是有效的 XML,因为您的 &lt;title&gt;&lt;description&gt; 节点中的 &amp;hellip; 实体。用&lt;![CDATA[tag]]&gt; 包裹这些字符串,然后重试。

标签: php rss


【解决方案1】:

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

它不是有效的 XML,因为您的 &lt;title&gt;&lt;description&gt; 节点中的 &amp;hellip; 实体。

&lt;![CDATA[tag]]&gt; 包装这些字符串,然后重试。

【讨论】:

    【解决方案2】:

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

    while ($row = mysql_fetch_assoc($result))

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

    【讨论】:

    • 在这种情况下,您的查询可能有问题。尝试在 mysql 命令行或 phpMyAdmin 中运行它以查看它返回的内容。
    • 正如yuli在他们的问题中所说,他们可以通过查看渲染页面的源来查看内容。
    • 感谢您的帮助,主要问题是我应该在我的项目标签中添加&lt;![CDATA[tag]]&gt;
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2012-03-09
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 2023-04-04
      • 2018-01-07
      相关资源
      最近更新 更多