【发布时间】: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…</description>
<link>http://picasaweb.google.com/roxyluvtony/KendraSpears#5551895410815389042</link>
<pubDate></pubDate>
</item>
...
</channel>
</rss>
【问题讨论】:
-
"为什么浏览器没有按照我想要的方式呈现这个 PHP 的输出?"不如“为什么这个 PHP 输出这个 XML 而不是我想要的 XML?”这样一个好问题。或“为什么这个 XML 没有按照我想要的方式在浏览器中呈现?”
-
@jnpcl,像这样
<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> -
@David Dorward,好的,我编辑了。
-
@yuli 您应该将 XML 编辑到问题中,这样很难阅读......它是否通过了 RSS 验证?您不应该发送 RSS 特定的内容类型吗?
-
乍一看,它不是有效的 XML,因为您的
<title>和<description>节点中的&hellip;实体。用<![CDATA[tag]]>包裹这些字符串,然后重试。