2011-09-29 2 views
1

내 웹 사이트에 RSS 피드를 포함하려고합니다. 다음 코드는 로컬로 작동하지만 라이브 사이트에 치명적인 오류가 발생합니다 : 내가 찾은 시행 착오 및 이와 유사한 질문을 찾는 많은 후PHP 치명적 오류 : 문자열을 XML로 구문 분석 할 수 없습니다.

[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 

: 이것은 다음과 같은 오류가 발생

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

문제를 일으키는 피드입니다. 피드를 매우 기본적인 샘플 느낌 (예 : http://feedparser.org/docs/examples/rss20.xml)으로 변경하면 모두 정상적으로 작동합니다. 구문 분석하려는 피드가 유효합니다 (경고가 있지만).

질문입니다 ... 스크립트를 피드를 받아들이려면 어떻게해야합니까?

+0

피드에 영향을 줍니까? 당신은 그것을 유효하게 할 수있다;) – TJHeuvel

답변

0

MIME 형식을 강제하는 다른 옵션이 있습니까?

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

EDIT : 로컬 환경에 비해 웹 서버의 cURL에도 문제가있을 수 있습니다. PHP 버전이 다른 경우 문제가 될 수 있습니다.

+0

트릭을하지 않았다 :(. PHP 버전은 다르다 : 5.3.2 로컬 vs 5.2.17 라이브. PHP를 로컬로 5.2.13으로 설정했다 - 피드가 여전히 표시되고있다. – rkhff

1

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

PHP가 이미 더 나은 통합 기능을 제공 할 때 맞춤 기능을 만드는 이유는 무엇입니까? 또한 인코딩 문제가 어떻게 나타나지 않습니까? –

+0

이 대답으로 저를 구해 줬습니다. – Luiz

관련 문제