Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load remote XML from Google App Engine for PHP

I want to load a remote and dynamic XML file from a third party server into my GAE-PHP application:

$itemId = 5;
$uri = "http://www.myserver.com/getInfoItem.php?itemId={$itemId}&format=xml";

I have tried to load the XML information using the simplexml_load_file function:

if ($xmlItem = simplexml_load_file($uri)) {
  // Code dealing with the XML info
}

But that leads always to this error:

PHP Warning: simplexml_load_file(): I/O warning : failed to load external entity "..."

So, I have changed the code and I try load the XML as a generic text file. This way, it works as expected:

if ($fileContents = file_get_contents($uri)) {
  $xmlItem = simplexml_load_string($fileContents);
  // Code dealing with the XML info
}

I was thinking the two functions get the remote contents using the same http wrapper, but that does not seem to work this way. I have had a look to to the GAE URL Fetch documentation, too.

My question is: Why the first approach does not work? Am I missing something?

like image 441
jap1968 Avatar asked Mar 10 '26 18:03

jap1968


1 Answers

We've disabled automatic loading of external entities by default, you have to opt in.

Try putting

libxml_disable_entity_loader(false);

before you're call. This is documented in the Disabled Functions section

This involves one additional step: First, you must create a php.ini file containing this line:

google_app_engine.enable_functions = "libxml_disable_entity_loader"
like image 179
Stuart Langley Avatar answered Mar 13 '26 07:03

Stuart Langley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!