close
ArticlesPHP

Resolving URL file access is disabled in the server configuration

I was trying out the simplexml_load_file function for parsing RSS feeds on my hosting server using the following code-

$rss =  simplexml_load_file(‘http://naveenbalani.com/index.php/feed/’);

and got the following exception on my hosting server –

“URL file access is disabled in the server configuration”

After googling through the documentation, I noticed that the simple_xml_load uses furl_open function to get files remotely. This function, due to security issues was not supported on my hosting server (and probably most of the hosting providers.)

The option was to use CURL library to parse remote urls , along with simple_xml function. Here is the modified code which should work on most of the hosting servers.

<?php

function load_file($url) {
$ch = curl_init($url);
#Return http response in string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = simplexml_load_string(curl_exec($ch));
return $xml;
}

$feedurl = 'http://naveenbalani.com/index.php/feed/';
$rss = load_file($feedurl);

foreach ($rss-&gt;channel-&gt;item as $item) {
echo "&lt;h2&gt;" . $item-&gt;title . "&lt;/h2&gt;";
echo "&lt;p&gt;" . $item-&gt;description . "&lt;/p&gt;";
}

>
Tags : phpsimplexml
Navveen

The author Navveen