2012-07-04 34 views
2

我正在使用以下代碼來讀取RSS提要並輸出結果。PHP 5.3中的simplexml_load_file錯誤

function home_page_parser($feedURL) { 
    $rss = simplexml_load_file($feedURL); 
    $i = 0; 

    echo "<ul>"; 

    foreach ($rss->channel->item as $feedItem) { 
     $i++; 
     $myDate = ($feedItem->pubDate); 
     $dateForm = explode(" ", $myDate); 
     echo "<li class=\"rss-feed\"><a href=\"$feedItem->link\" title=\"$feedItem->title\" target=\"_blank\">".$feedItem->title."</a><br />" .$feedItem->pubDate. "</li>"; 

    if($i >= 3) break; 

    echo "</ul>"; 
    } 
} 

這是我的測試站點Rackspace的雲上運行PHP 5.2

論媒體寺直播站點上運行PHP 5.3的工作很好,我得到以下錯誤:


警告:simplexml_load_file()[function.simplexml-load-file]:在服務器配置中,通過allow_url_fopen = 0在第39行的/.../html/includes/functions.php中禁用http://封裝器

警告:使用simplexml_load_file(http://www.chinaknowledge.com/Newswires/RSS_News/RSS_News.xml)[function.simplexml負荷文件]:未能打開流:沒有合適的包裝可在/.../html/includes/functions.php找到第39行上

警告:simplexml_load_file()[function.simplexml-load-file]:I/O警告:未能加載外部實體"http://www.chinaknowledge.com/Newswires/RSS_News/RSS_News.xml" in /.../html/includes/functions.php on line 39

Warning:Invalid argument提供給foreach()在線44上的/.../html/includes/functions.php


第39行是這樣的:

$rss = simplexml_load_file($feedURL); 

什麼我做錯了或需要改變對5.3工作?

回答

5

該錯誤是相當具有描述性的,你不覺得嗎?

的http://封裝在服務器配置由 禁用於allow_url_fopen = 0

您將需要編輯PHP配置文件並更改配置allow_url_fopen如果你不能做到這一點直接嘗試的ini_set()

編輯:作爲@evanmcd在評論中指出,這種配置只能在php.ini中設置。 PHP documentation參考。

+0

我可以添加到我的功能文件嗎? – Brett

+0

是的。嘗試添加到頂部。但在許多共享主機環境中,函數'ini_set'也被禁用。 – shxfee

+0

試圖把功能文件,並沒有運氣。它在php.ini文件中被關閉並打開它,沒有運氣......我會去和Media Temple聊天,看看我能找到什麼。 – Brett

1

此錯誤是由於「http:// wrapper在服務器配置中被allow_url_fopen = 0禁用」。爲避免此問題,我們需要將此設置覆蓋爲On而不是。在我看來,大多數共享託管服務器不允許你通過ini_set('allow_url_fopen','on');或htaccess覆蓋。因此,我不建議嘗試這些方法,而是建議一種獲取該提要的方法如下。使用CURL,我們需要將提要xml的內容提取到變量中。然後處理我們的simplexml文件操作。

$feed ='http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=mytwittername'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $feed); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
// get the result of http query 
$output = curl_exec($ch); 
curl_close($ch); 
$xml = simplexml_load_file($output); 
+0

對於我來說,最後一行需要'$ xml = simplexml_load_string($ output)' – Sparky

0
ini_set("allow_url_fopen", 1); 

這將設置允許URL在PHP開放=開。ini文件,但你需要在easyphp或xamp或wamp或託管中重新啓動php。

1

如果您不允許在服務器中編輯php.ini,您可以使用curl獲取xml並讀取xml stirng,如下所示。

function home_page_parser($feedURL) { 
    $rss = simplexml_load_file(curlXML($feedURL); 
    $i = 0; 

    echo "<ul>"; 

    foreach ($rss->channel->item as $feedItem) { 
     $i++; 
     $myDate = ($feedItem->pubDate); 
     $dateForm = explode(" ", $myDate); 
     echo "<li class=\"rss-feed\"><a href=\"$feedItem->link\" title=\"$feedItem->title\" target=\"_blank\">".$feedItem->title."</a><br />" .$feedItem->pubDate. "</li>"; 

    if($i >= 3) break; 

    echo "</ul>"; 
    } 
} 

function curlXML($url){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    // get the result of http query 
    $output = curl_exec($ch); 
    curl_close($ch); 

    return $output; 
}