2011-09-29 152 views
1

我试图在我的网站上添加一个RSS订阅源。下面的代码在本地工作,但会导致直播现场上一个致命的错误:PHP致命错误:字符串无法解析为XML

<?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是有效的(虽然有一些警告)。

的问题是...我需要做什么拿到剧本接受饲料?

+0

你对饲料造成任何影响?你可以使它有效;) – TJHeuvel

回答

0

也许另一个选项强制MIME类型?

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

编辑:也可能是您的web服务器上的cURL与您的本地环境相比的问题。如果PHP版本不同,那可能是一个问题。使用mb_convert_encoding(),不要忘记调用parseRSS()函数

+0

没有做到:(PHP的版本不同:5.3.2本地vs 5.2.17 live。我已经将PHP设置为本地5.2.13 - Feed仍在显示 – rkhff

1

更改为utf8。

// 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> 
+0

为什么在PHP已经有了更好的集成的函数的时候创建一个自定义的函数呢?我还没有看到这是一个编码问题? –

+0

这个答案救了我。 – Luiz