Jump to content


Photo

Yikes! simplexml_load_file() is failing.


  • Please log in to reply
8 replies to this topic

#1 Michael Morrison

Michael Morrison

    Active Member

  • O'Reilly Author
  • PipPipPipPip
  • 114 posts
  • Location:Nashville, TN
  • Interests:nerdy tinkering, skateboarding, hockey, pond building, landscaping, architecture/construction

Posted 12 January 2009 - 09:21 AM

Enduring proof that none of us is immune to unforeseen problems, even when you wrote the book on it! So I obviously have my own web server for building and testing the example code in the book. And recently I needed to test something out in the Aliens Abducted Me example. So I navigate to the app in my browser just to make sure all is well before I start tinkering with the code, and surprise, the YouTube feed is broken!

Now keep in mind I haven't touched the code on the server, so I did nothing to cause it to now fail. The problem is that my server is actually on a shared host, and is potentially subject to the hosting company making configuration changes without me necessarily knowing about it. And for some reason they decided to tighten up some of the PHP security settings. The end result is that the simplexml_load_file() function won't work - I get this warning:

Warning: simplexml_load_file() [function.simplexml-load-file]: URL file-access is disabled in the server configuration in .../ch12/final/aliens/youtube.php on line 21


If you're getting this warning instead of seeing YouTube videos at the bottom of Aliens Abducted Me, don't despair. The same problem just bit me and there is a work-around. The best fix is to ask your web hosting company or web server admin to change a PHP configuration setting to allow the simplexml_load_file() function to work. If they're willing, ask them to change the allow_url_fopen setting in the php.ini file to ON. But there's a good chance they won't be so helpful, in which case you need to go to option 2, as I did.

Option 2 is to sidestep simplexml_load_file() entirely, and use a different approach to loading XML data. The approach involves using the Client URL library, also known as cURL, which is built into PHP. cURL is extremely flexible, allowing you to communicate with all kinds of different servers using lots of different protocols. In our case, we just want to use cURL to load a file from a URL, and then we can pull it into an XML object. Here's a reusable PHP function that uses cURL to load any kind of data from a URL:

  function load_file_from_url($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_REFERER, 'http://www.headfirstlabs.com/');
    $str = curl_exec($curl);
    curl_close($curl);
    return $str;
  }


Change the URL in the line with the Head First Labs URL to your own - this just lets the server know who generated the request (some servers, like Google News, require you to provide this). Now we need one more function to actually load an XML file from a URL into an XML object in PHP:

  function load_xml_from_url($url) {
    return simplexml_load_string(load_file_from_url($url));
  }


Stick those two functions in your PHP code, or into a reusable script that you pull in with require_once, and you're ready to roll. Then just replace the call to simplexml_load_file() to load_xml_from_url():

  $xml = load_xml_from_url(YOUTUBE_URL);


That's it! Now, just to show the flexibility of reusable functions, there's a chance you may want to load in other kinds of data using the same approach. As an example, you might find a data source that provides data in the JavaScript Object Notation (JSON) format, such as Google News. This function takes care of loading data from a URL into a JSON object:

  function load_json_from_url($url) {
    return json_decode(load_file_from_url($url));
  }


It reuses our general load_file_from_url() function to keep from duplicating the cURL loading code. To see this function in action with JSON data, check out this post.
Michael Morrison
Author of Head First JavaScript, Head First PHP & MySQL

#2 lemuel

lemuel

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 577 posts

Posted 12 January 2009 - 01:09 PM

1 vote for this as a sticky...

#3 Michael Morrison

Michael Morrison

    Active Member

  • O'Reilly Author
  • PipPipPipPip
  • 114 posts
  • Location:Nashville, TN
  • Interests:nerdy tinkering, skateboarding, hockey, pond building, landscaping, architecture/construction

Posted 12 January 2009 - 01:17 PM

(lemuel)
1 vote for this as a sticky...


Done!
Michael Morrison
Author of Head First JavaScript, Head First PHP & MySQL

#4 SPDworks

SPDworks

    New Member

  • Members
  • Pip
  • 4 posts

Posted 22 April 2009 - 08:01 AM

Ahha!

Can't wait until I get to where this all makes better sense to me. That's why I bought your book, actually.

Unfortunately, at this point, I couldn't manage to find any ongoing reliable help in installing the componets for the suggested Win32 apache,mysql/php setup, so I ended up going with the XAMPP package. It's gorgeous and works, actually, or rather that's basically what it says in all the GUI stuff it comes with. Now, I would just like to know #1: how to upload to the server, all the php files and such for lesson one in your book, and #2: whether downloading and extracting/saving the Ch01 files for the lesson must be done (saved into or as) a certain file name. In other words, can the downloaded/extracted Ch01 files be changed (per the book's instructions,) and then saved in a whole other file folder, with filenames for the changed files to reflect they're not the originals (to preserve the original downloaded files?)

QUOTE (mmorrison @ Jan 12 2009, 09:21 AM) <{POST_SNAPBACK}>
Enduring proof that none of us is immune to unforeseen problems, even when you wrote the book on it! So I obviously have my own web server for building and testing the example code in the book. And recently I needed to test something out in the Aliens Abducted Me example. So I navigate to the app in my browser just to make sure all is well before I start tinkering with the code, and surprise, the YouTube feed is broken!

Now keep in mind I haven't touched the code on the server, so I did nothing to cause it to now fail. The problem is that my server is actually on a shared host, and is potentially subject to the hosting company making configuration changes without me necessarily knowing about it. And for some reason they decided to tighten up some of the PHP security settings. The end result is that the simplexml_load_file() function won't work - I get this warning:

CODE
Warning: simplexml_load_file() [function.simplexml-load-file]: URL file-access is disabled in the server configuration in .../ch12/final/aliens/youtube.php on line 21


If you're getting this warning instead of seeing YouTube videos at the bottom of Aliens Abducted Me, don't despair. The same problem just bit me and there is a work-around. The best fix is to ask your web hosting company or web server admin to change a PHP configuration setting to allow the simplexml_load_file() function to work. If they're willing, ask them to change the allow_url_fopen setting in the php.ini file to ON. But there's a good chance they won't be so helpful, in which case you need to go to option 2, as I did.

Option 2 is to sidestep simplexml_load_file() entirely, and use a different approach to loading XML data. The approach involves using the Client URL library, also known as cURL, which is built into PHP. cURL is extremely flexible, allowing you to communicate with all kinds of different servers using lots of different protocols. In our case, we just want to use cURL to load a file from a URL, and then we can pull it into an XML object. Here's a reusable PHP function that uses cURL to load any kind of data from a URL:

CODE
  function load_file_from_url($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_REFERER, 'http://www.headfirstlabs.com/');
    $str = curl_exec($curl);
    curl_close($curl);
    return $str;
  }


Change the URL in the line with the Head First Labs URL to your own - this just lets the server know who generated the request (some servers, like Google News, require you to provide this). Now we need one more function to actually load an XML file from a URL into an XML object in PHP:

CODE
  function load_xml_from_url($url) {
    return simplexml_load_string(load_file_from_url($url));
  }


Stick those two functions in your PHP code, or into a reusable script that you pull in with require_once, and you're ready to roll. Then just replace the call to simplexml_load_file() to load_xml_from_url():

CODE
  $xml = load_xml_from_url(YOUTUBE_URL);


That's it! Now, just to show the flexibility of reusable functions, there's a chance you may want to load in other kinds of data using the same approach. As an example, you might find a data source that provides data in the JavaScript Object Notation (JSON) format, such as Google News. This function takes care of loading data from a URL into a JSON object:

CODE
  function load_json_from_url($url) {
    return json_decode(load_file_from_url($url));
  }


It reuses our general load_file_from_url() function to keep from duplicating the cURL loading code. To see this function in action with JSON data, check out this post.



#5 SPDworks

SPDworks

    New Member

  • Members
  • Pip
  • 4 posts

Posted 22 April 2009 - 08:03 AM

QUOTE (mmorrison @ Jan 12 2009, 01:17 PM) <{POST_SNAPBACK}>
Done!


I second the motion...yes yes. And if my questions in my first reply are answered, I suppose they can be moved elsewhere, if this post by the author IS stickied.


#6 drewdin

drewdin

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 608 posts
  • Gender:Male
  • Location:Boston

Posted 04 June 2010 - 04:39 AM

I have this problem now, I called GoDaddy and they said I need to create a php5.ini and upload to my root folder. Then I can change what ever i want, that does not sound right to me?

Anyone have any insight? Thanks


#7 pinginsehat

pinginsehat

    New Member

  • Members
  • Pip
  • 2 posts
  • Location:http://pinginsehat.info

Posted 04 August 2010 - 09:13 AM

QUOTE (drewdin @ Jun 4 2010, 04:39 AM) <{POST_SNAPBACK}>
I have this problem now, I called GoDaddy and they said I need to create a php5.ini and upload to my root folder. Then I can change what ever i want, that does not sound right to me?

Anyone have any insight? Thanks

is this php.ini setting helping you?

#8 drewdin

drewdin

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 608 posts
  • Gender:Male
  • Location:Boston

Posted 26 October 2010 - 10:59 AM

it did help, it is just weird how it had to be done

#9 mcrahoof786

mcrahoof786

    New Member

  • Members
  • Pip
  • 2 posts

Posted 24 January 2011 - 04:04 AM

This codes are very useful to learn about Youtube , this really helped me !!!!!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users