Search code examples
simplexmlphp

simplexml_load_file error in PHP 5.3


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?


Solution

  • 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.