我使用以下脚本为我的网站生成RSS源:
<?php
class RSS
{
public function RSS()
{
$root = $_SERVER['DOCUMENT_ROOT'];
require_once ("../connect.php");
}
public function GetFeed()
{
return $this->getDetails() . $this->getItems();
}
private function dbConnect()
{
DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
}
private function getDetails()
{
$detailsTable = "rss_feed_details";
$this->dbConnect($detailsTable);
$query = "SELECT * FROM ". $detailsTable ." WHERE feed_category = ''";
$result = mysql_db_query (DB_NAME, $query, LINK);
while($row = mysql_fetch_array($result))
{
$details = '<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>'. $row['title'] .'</title>
<link>'. $row['link'] .'</link>
<description>'. $row['description'] .'</description>
<language>'. $row['language'] .'</language>
<image>
<title>'. $row['image_title'] .'</title>
<url>'. $row['image_url'] .'</url>
<link>'. $row['image_link'] .'</link>
<width>'. $row['image_width'] .'</width>
<height>'. $row['image_height'] .'</height>
</image>';
}
return $details;
}
private function getItems()
{
$itemsTable = "rss_posts";
$this->dbConnect($itemsTable);
$query = "SELECT * FROM ". $itemsTable ." ORDER BY id DESC";
$result = mysql_db_query (DB_NAME, $query, LINK);
$items = '';
while($row = mysql_fetch_array($result))
{
$items .= '<item>
<title>'. $row["title"] .'</title>
<link>'. $row["link"] .'</link>
<description><![CDATA['.$row["readable_date"]."<br /><br />".$row["description"]."<br /><br />".']]></description>
</item>';
}
$items .= '</channel>
</rss>';
return $items;
}
}
?>
令人费解的是,脚本在我的localhost上运行得很好但在我的远程服务器上出现以下错误:
XML Parsing Error: junk after document element
Location: http://mysite.com/rss/main/
Line Number 2, Column 1:<b>Parse error</b>: syntax error, unexpected T_STRING in <b>/home/studentw/public_html/rss/global-reach/rssClass.php</b> on line <b>1</b><br />
^
答案 0 :(得分:0)
您可能有一些无关紧要的字符,例如标签或空格,它们会与远程服务器上的xml文件一起回显。在打印xml之前,我会使用输出缓冲来清理它。
更新1
这是来自output buffering上的PHP网站:
<?php
function callback($buffer)
{
// replace all the apples with oranges
return (str_replace("apples", "oranges", $buffer));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php
ob_end_flush();
?>
您可以搜索空格和标签,而不是替换苹果和橙子。
答案 1 :(得分:0)
尝试为响应发送正确的内容类型:
您可以尝试以下操作:
header("Content-Type:TYPE");
TYPE是以下之一:
text/xml
application/rss+xml
application/rdf+xml
application/atom+xml