2013-02-19 110 views

回答

1

首先,您需要啓用錯誤記錄和/或報告以瞭解更多信息。你也可以檢查從返回值的一些參數和遠程請求:

$result = simplexml_load_file($url); 

var_dump($result, $http_response_header); 

這會告訴你,加載失敗,錯誤消息告訴你爲什麼失敗:

PHP的警告:使用simplexml_load_file():http://www.nationnews.com/site/feed/:1:分析器錯誤:開始標記預期, '<' 在/example.php沒有發現10號線
PHP的警告:使用simplexml_load_file():在/example.php在線10
PHP的警告:使用simplexml_load_file( ):^在/例子中。PHP的第10行

而且$http_response_header還告訴你什麼是已經從主機返回的畫面:

array(23) { 
    [0]=> string(15) "HTTP/1.0 200 OK" 
    [1]=> string(35) "Date: Wed, 20 Feb 2013 10:56:11 GMT" 
    [2]=> string(30) "Server: Apache/2.2.15 (CentOS)" 
    [3]=> string(23) "X-Powered-By: PHP/5.3.3" 
    [4]=> string(56) "Set-Cookie: PHPSESSID=qeaq20mrvrc2u4c403sou6oro2; path=/" 
    [5]=> string(38) "Expires: Wed, 20 Feb 2013 08:13:01 GMT" 
    [6]=> string(50) "Cache-Control: no-store, no-cache, must-revalidate" 
    [7]=> string(16) "Pragma: no-cache" 
    [8]=> string(84) "Set-Cookie: exp_last_visit=1046015771; expires=Thu, 20-Feb-2014 10:56:11 GMT; path=/" 
    [9]=> string(87) "Set-Cookie: exp_last_activity=1361375771; expires=Thu, 20-Feb-2014 10:56:11 GMT; path=/" 
    [10]=> string(89) "Set-Cookie: exp_tracker=a%3A1%3A%7Bi%3A0%3Bs%3A11%3A%22%2Fsite%2Ffeed%2F%22%3B%7D; path=/" 
    [11]=> string(44) "Last-Modified: Wed, 20 Feb 2013 07:13:01 GMT" 
    [12]=> string(40) "Cache-Control: post-check=0, pre-check=0" 
    [13]=> string(21) "Vary: Accept-Encoding" 
    [14]=> string(16) "imagetoolbar: no" 
    [15]=> string(17) "Connection: close" 
    [16]=> string(37) "Content-Type: text/xml; charset=utf-8" 
    [17]=> string(76) "Set-Cookie: cookiesession1=HTEVZV0HJNK2HARUL2QBDADH8RXYESJB;Path=/;HttpOnly " 
    [18]=> string(109) "Set-Cookie: exp_last_visit_cookiesession2=tSzDt6/k20k=;Expires=Thu, 20-Feb-2014 10:56:11 GMT;Path=/;HttpOnly " 
    [19]=> string(112) "Set-Cookie: exp_last_activity_cookiesession2=e+FVcqI8+Ck=;Expires=Thu, 20-Feb-2014 10:56:11 GMT;Path=/;HttpOnly " 
    [20]=> string(68) "Set-Cookie: exp_tracker_cookiesession2=w+13lT4TxY0=;Path=/;HttpOnly " 
    [21]=> string(22) "Content-Encoding: gzip" 
    [22]=> string(20) "content-length: 2463" 
} 

根據HTTP規範,您使用的通過使用HTTP URI時,內容編碼爲:

[21]=> string(22) "Content-Encoding: gzip" 

這不是由PHP與它支持開箱即用的HTTP包裝,所以你需要解決的是你自己的

例如通過使用zlib Stream Wrapper:

$result = simplexml_load_file('compress.zlib://' . $url); 

或者與gzdecode功能的幫助:

$result = simplexml_load_string(gzdecode(file_get_contents($url))); 

完整的示例代碼,所有的變種:

$url = 'http://www.nationnews.com/site/feed/'; 


// stream wrapper: 

$result = simplexml_load_file('compress.zlib://' . $url); 

var_dump($result, $http_response_header); 


// gzdecode: 

$result = simplexml_load_string(gzdecode(file_get_contents($url))); 

var_dump($result, $http_response_header); 


// none (the error case): 

$result = simplexml_load_file($url); 

var_dump($result, $http_response_header); 

相關問題:

而下面的PHP bug報告有關(只挑了些,這很可能是不完整):

+0

我將確保啓用錯誤檢查 – user1807206 2013-02-20 21:20:36

+0

@ user1807206:是的,對於開發,啓用所有錯誤。這將爲你節省很多時間尋找錯誤的地方。你可能會發現這很有用:[如何獲得有用的錯誤消息在PHP?](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) - 這是一個有點範圍廣泛,並有不同的答案,但它有點顯示了不同的可能性。更多的動手是[我留在它的答案](http://stackoverflow.com/a/14504459/367456)。 – hakre 2013-02-21 08:05:54

1

rss feed是gziped。這應該做的伎倆:

$content = file_get_contents("http://www.nationnews.com/site/feed/"); 
$rss = simplexml_load_string(gzinflate(substr($content,10,-8))); 

,瞭解有關gzinflate進一步詳情,請參閱PHP: Call to undefined function gzdecode()

+0

謝謝,那很容易。無法在任何地方找到答案 – user1807206 2013-02-20 21:07:28