PHP 致命错误:字符串无法解析为 XML

发布于 2024-12-06 22:19:42 字数 2190 浏览 0 评论 0原文

我正在尝试在我的网站上包含 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)一切正常。我尝试解析的提要是有效的(尽管有一些警告)。

问题是...我需要做什么才能让脚本接受提要?

I'm trying to include an RSS feed on my website. The following code works locally but causes a fatal error on the live site:

<?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>

This causes the following errors:

[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

After lots of trial and error and looking up similar questions I've found it's the feed that's causing the problem. If I change the feed to a very basic sample feel (for instance http://feedparser.org/docs/examples/rss20.xml) it all works fine. The feed I'm trying to parse is valid (though has some warnings).

Question is... what do I need to do get the script to accept the feed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独木成林 2024-12-13 22:19:42

使用 mb_convert_encoding() 更改为 utf8,并且不要忘记调用 parseRSS() 函数。

// 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:
$enc = mb_detect_encoding($data);
$data = mb_convert_encoding($data, 'UTF-8', $enc);

// 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";
}
parseRSS($doc);
?>
<!doctype html>
<html lang="en-GB">
<head>
 <meta charset="UTF-8" />
 <title>Test feed</title>
</head>
<body>

Change to utf8 using mb_convert_encoding() and don't forget to call the parseRSS() function.

// 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:
$enc = mb_detect_encoding($data);
$data = mb_convert_encoding($data, 'UTF-8', $enc);

// 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";
}
parseRSS($doc);
?>
<!doctype html>
<html lang="en-GB">
<head>
 <meta charset="UTF-8" />
 <title>Test feed</title>
</head>
<body>
待"谢繁草 2024-12-13 22:19:42

也许还有另一种强制 MIME 类型的选择?

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml'));

编辑:与本地环境相比,您的网络服务器上的 cURL 也可能存在问题。如果 PHP 版本不同,那么这可能是一个问题。

Maybe another option to force the MIME type?

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml'));

EDIT: Could also be a problem with cURL on your webserver compared to your local environment. If the PHP versions are different then that could be an issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文