【发布时间】:2012-01-17 08:09:39
【问题描述】:
有问题的提要是:http://api.inoads.com/snowstorm/feed.xml
这是我用于生成的 PHP 代码:
<?php
$database = 'xxxx';
$dbconnect = mysql_pconnect('xxxx', 'xxxx', 'xxxx');
mysql_select_db($database, $dbconnect);
$query = "SELECT * FROM the_queue WHERE id LIKE '%' ORDER BY id DESC LIMIT 25";
$result = mysql_query($query, $dbconnect);
while ($line = mysql_fetch_assoc($result))
{
$return[] = $line;
}
$now = date("D, d M Y H:i:s T");
$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<rss version=\"2.0\">
<channel>
<title>The Queue</title>
<link>http://readapp.net</link>
<description>A curated reading list.</description>
<language>en-us</language>
<pubDate>$now</pubDate>
<lastBuildDate>$now</lastBuildDate>
";
foreach ($return as $line)
{
$output .= "<item><title>".htmlspecialchars($line['title'])."</title>
<description>".htmlspecialchars($line['description'])."</description>
<link>".htmlspecialchars($line['link'])."</link>
<pubDate>".htmlspecialchars($line['pubDate'])."</pubDate>
</item>";
}
$output .= "</channel></rss>";
$fh = fopen('feed.xml', 'w');
fwrite($fh, $output);
?>
什么可能导致错误?
这是来自提要验证器的链接:http://validator.w3.org/feed/check.cgi?url=http%3A%2F%2Fapi.inoads.com%2Fsnowstorm%2Ffeed.xml
【问题讨论】:
-
欢迎来到 Stack Overflow!您没有在查询中进行任何错误检查。在
mysql_query()通话后,您需要执行此操作。否则,如果查询失败,您的脚本将中断。 manual onmysql_query()或此reference question. 中概述了如何执行此操作 -
你的数据是什么字符串编码的?您需要在
<?xml>标记中指定它。例如<?xml version="1.0" encoding="..."?> -
@AbhiBeckert UTF-8 - 我已经修改了上面的帖子以反映这一点
-
@deceze 引号和问号存在问题 - 我已更新帖子以显示这一点。
-
mysql 扩展已过时,即将弃用。新代码应该使用mysqli或PDO,两者都有重要的优势,比如支持prepared statements。
标签: php mysql xml utf-8 character-encoding