Dec 16 2008

Has your host got allow_url_fopen = Off and they’re too stubborn to turn it on? Got this error?

Warning: simplexml_load_file() [function.simplexml-load-file]: URL file-access is disabled in the server configuration in /url/to/file.php on line 62

It’s a pain for writing cool API clients that add cool functionality into your website, like using the Last.fm API, Amazon Associates Web Services, or the YouTube API, because they all use XML based services that you have to retrieve.

The simplest way is to use file_get_contents($url);, but that doesn’t work if allow_url_fopen = Off.

The next simplest way is to use libcurl, which although is very useful, has given me a lot of grief throughout my programming life, so is sort of my arch nemesis. I tried it on 1and1 trying to access AAWS, but it didn’t work anyway.

The last solution is my hacky little method to draw wget results into the script. This method is probably the slowest as we’re calling an external binary on a shared web server, but it works.

$url = "http://api.somesite.com/rest/xml?query=string&goes=here";
$user_agent = "My Awesome API Client v1.0";
ob_start();
passthru("wget -U '{$user_agent}' -q -O - '{$url}'");
$response = ob_get_contents();
ob_end_clean();
$xml_document = simplexml_load_string($response);

2 Responses to “Getting Around allow_url_fopen = Off”

  1. Chris Dean says:

    That was the whole point of this:
    http://www.refactormycode.com/codes/440-universal-file-download-class

    And I quote:


    James
    August 19, 2008, 4 months ago, permalink
    Spinner No rating. Login to rate!

    I wish I had this last night… gah!

    Feel free to improve on it ;)

  2. James says:

    Indeed, I wrote this bit of code that night, and found your class after, but either will work just fine.

    I only posted this because someone I host a website for asked a way around simplexml_load_file with a remote URL, and I gave him the solution I used.

Leave a Reply