最近我在网站的RSS中遇到了一个异常的事情。
在rss页面源的开头有一个额外的空格字符,由于下面的错误,浏览器无法显示它:
error on line 1 at column 6: XML declaration allowed only at the start of the document
我的代码很简单,我在其他网站上使用时没有问题
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<rss version=\"2.0\" \n";
echo " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n";
echo " xmlns:sy=\"http://purl.org/rss/1.0/modules/syndication/\"\n";
echo " xmlns:admin=\"http://webns.net/mvcb/\"\n";
echo " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >\n\n";
echo "<channel>\n";
echo "<title>".htmlspecialchars($sitename)."</title>\n";
echo "<link>".htmlspecialchars($url)."</link>\n";
echo "<description>".$slogan."</description>\n";
echo "<dc:language>".$backend_language."</dc:language>\n";
echo "<dc:creator>".$adminmail."</dc:creator>\n";
echo "<dc:date>".$now."</dc:date>\n\n";
echo "<sy:updatePeriod>hourly</sy:updatePeriod>\n";
echo "<sy:updateFrequency>1</sy:updateFrequency>\n";
echo "<sy:updateBase>".$now."</sy:updateBase>\n\n";
结果如下:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<channel>
正如您之前所见
我该怎么做才能解决这个问题!?