I am using the following script to generate a RSS feed for my site:
<?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 = ''";
开发者_JS百科 $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;
}
}
?>
The baffling thing is, the script works perfectly fine on my localhost but gives the following error on my remote server:
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 />
^
You probably have some extraneous character like a tab or a space that is getting echoed out with the xml file on your remote server. I'd use output buffering to clean that up before printing the xml.
Update 1
This is from the PHP website on output buffering:
<?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();
?>
Instead of replacing apples and oranges, you could search for spaces and tabs.
Try sending the correct content type for the response:
You can try the following:
header("Content-Type:TYPE");
Where TYPE is one of the following:
text/xml
application/rss+xml
application/rdf+xml
application/atom+xml
精彩评论