【发布时间】:2011-11-27 17:27:10
【问题描述】:
我正在尝试在我的网站上包含一个 RSS 提要。以下代码可以在本地运行,但会在实时站点上导致致命错误:
<?php
// Initialise the cURL resource handle:
$ch = curl_init("http://www.blogs.stopjunkmail.org.uk/diary/index.php?/feeds/index.rss2");
// Set connection options:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
// Execute connection, wait for response, and close:
$data = curl_exec($ch);
curl_close($ch);
// Parse the data:
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
// Define the function to parse RSS:
function parseRSS($doc) {
echo '<ul>' . "\n";
for($i=0; $i<5; $i++) {
$url = $doc->channel->item[$i]->link;
$title = $doc->channel->item[$i]->title;
$date = $doc->channel->item[$i]->pubDate;
echo '<li>' . "\n";
echo '<a href="'.$url.'">'.$title.'</a>' . "\n";
echo '</li>' . "\n";
}
echo '</ul>' . "\n";
}
?>
<!doctype html>
<html lang="en-GB">
<head>
<meta charset="UTF-8" />
<title>Test feed</title>
</head>
<body>
<h2>Recent blog entries</h2>
<?php parseRSS($doc); ?>
</body>
</html>
这会导致以下错误:
[Thu Sep 29 12:06:28 2011] [error] [client xx.xx.xx.xxx] PHP Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/sites/stopjunkmail.org.uk/public_html/news/_test.php:11
[Thu Sep 29 12:06:28 2011] [error] [client xx.xx.xx.xxx] Stack trace:
[Thu Sep 29 12:06:28 2011] [error] [client xx.xx.xx.xxx] #0 /home/sites/stopjunkmail.org.uk/public_html/news/_test.php(11): SimpleXMLElement->__construct('', 16384)
[Thu Sep 29 12:06:28 2011] [error] [client xx.xx.xx.xxx] #1 {main}
[Thu Sep 29 12:06:28 2011] [error] [client xx.xx.xx.xxx] thrown in /home/sites/stopjunkmail.org.uk/public_html/news/_test.php on line 11
经过大量试验和错误并查找类似问题后,我发现是导致问题的提要。如果我将提要更改为非常基本的样本感觉(例如http://feedparser.org/docs/examples/rss20.xml),一切正常。我尝试解析的提要有效(尽管有一些警告)。
问题是...我需要做什么才能让脚本接受提要?
【问题讨论】:
-
你对feed有影响吗?你可以让它有效;)