Warning: simplexml_load_file() [function.simplexml-load-file]: URL file-access is disabled in the server configuration


If you’ve seen that error message you’ve probably happened upon a security feature that your shared web hosting provider has enabled. There are a few work-arounds for this error but most require you to have certain privileges on the server that you probably don’t have. Quite frankly, if you are getting these errors you probably don’t have the ability to change these settings yourself.

Rather than try to get the provider to change these settings (let’s face it, they have this enabled for a reason and surely someone else has already tried to get this changed, right?) one can easily get around this with Curl. In most cases, curl will be enabled on the server. So here is the quick and dirty way to get around it:

Create a PHP file and name it anything you want. For the sake of this article we’ll refer to it as curl_functions.php. In this file put the following functions:

<php
function setupMyCurl() {
   $myCurl = curl_init(); 
   $temp = curl_setopt($myCurl, CURLOPT_RETURNTRANSFER, 1);
   return($myCurl);
}
define("myCurl", setupMyCurl());
function curl_get_contents($url) {
   $temp = curl_setopt(myCurl, CURLOPT_URL, $url);
   return(curl_exec(myCurl));
}
?>

Include or require this file. Then, all you have to do is use the curl_get_contents($url) in your code to pull in the xml to a string. Then use the simplexml_load_string() instead of simplexml_load_file(). This will give you the same results but works around the url fopen feature. If you don’t have curl enabled on your host, GET ANOTHER HOST. 🙂

, ,

  1. #1 by Barry Walsh on March 29, 2012 - 4:30 am

    Cheers for this, first solution I’ve read that didn’t say to ask your host to enable allow_url_fopen. Worked great, so thanks!

    • #2 by Randy on March 29, 2012 - 4:52 am

      Glad to help! I use this all the time, so I know it’s handy!

(will not be published)