simplexml_load_file error in PHP 5.3

笑着哭i 提交于 2019-12-24 02:45:15

问题


I'm using the following code to read an RSS feed and output the results.

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>";
    }
}

It is working fine on my testing site at Rackspace Cloud running PHP 5.2

On the live site at Media Temple running PHP 5.3, I get the following errors:


Warning: simplexml_load_file() [function.simplexml-load-file]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /.../html/includes/functions.php on line 39

Warning: simplexml_load_file(http://www.chinaknowledge.com/Newswires/RSS_News/RSS_News.xml) [function.simplexml-load-file]: failed to open stream: no suitable wrapper could be found in /.../html/includes/functions.php on line 39

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://www.chinaknowledge.com/Newswires/RSS_News/RSS_News.xml" in /.../html/includes/functions.php on line 39

Warning: Invalid argument supplied for foreach() in /.../html/includes/functions.php on line 44


Line 39 is this:

$rss = simplexml_load_file($feedURL);

What am I doing wrong or needs to change to work on 5.3?


回答1:


The error is pretty descriptive dont you think?

http:// wrapper is disabled in the server configuration by allow_url_fopen=0

You will need to edit the PHP configuration file and change the configuration allow_url_fopen. If you cant do this directly try ini_set()

Edit: As @evanmcd pointed out in the comments, this configuration can only be set in php.ini. PHP documentation reference.




回答2:


This error comes due to "http:// wrapper is disabled in the server configuration by allow_url_fopen=0" .For avoiding this issue we need to override this setting to On instead off.In my view most of shared hosting servers do not allow you to do these setting through either ini_set('allow_url_fopen', 'on'); or htaccess overriding.So instead of trying these methods I suggest a way to fetch that feed is as follows.Using CURL we need to fetch the content of feed xml to a variable.Then process our simplexml file operations .

Example

$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);



回答3:


If you are not allowed to edit php.ini in server you can use curl to get xml and read xml stirng as below.

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;
}



回答4:


ini_set("allow_url_fopen", 1);

This will set allow url open = On in php.ini file but you need to restart php in easyphp or xamp or wamp or in hosting.



来源:https://stackoverflow.com/questions/11335187/simplexml-load-file-error-in-php-5-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!