2

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?

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Can I add that to my functions file?
yes. try adding that to the top. but the function ini_set is also disabled in a lot of shared hosting environments.
Tried putting in the functions file and no luck. It was turned off in the php.ini file and turned it on and no luck... I'll go chat with Media Temple and see what I can find.
did the error message change? Could you run the function phpinfo() and check to see for sure if it is turned on
allow_url_fopen can't be set via ini_set(). See stackoverflow.com/questions/1804010/…
|
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);

1 Comment

For me, the last line needed to be $xml = simplexml_load_string($output)
1

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.