tags:

views:

20

answers:

2
$doc = new DOMDocument();
if ($doc->load('http://foo.com/bar.xml')) {
  // good
} else {
  // wtf happened?
}

I can wget http://foo.com/bar.xml from the location where the PHP code is running, so I know the URL is accessible. I'm thinking it must be something other than an HTTP error.

I'm not sure what else could be causing the failure. Maybe a parsing issue? The XML appears to be valid (and passes W3C's validation test). As far as I can tell from the documentation, there's no way to determine why the load failure occurred.

Here's the XML:

 <response> 
  <version>8</version> 
  <minversion>1</minversion> 
  <url>api.asp?</url> 
 </response>
+1  A: 

Add this:

    libxml_use_internal_errors ( true );
    $doc = new DOMDocument();
    $doc -> recover = true;
    $doc -> strictErrorChecking = false;
stillstanding
What's this do for me? (I'm not a PHP guy, this is a one-off thing I'm working on)
Rob Sobers
it simply suppresses errors and allows your code to continue in case of failure. to check what the exact problem is, append the following: `var_dump ( libxml_get_errors () );`
stillstanding
@stillstanding Oh, thanks! My code continues (it hits the `else` clause), but I need to know why. Hopefully the `var_dump` will tell me what's wrong.
Rob Sobers
@stillstanding So, that just gave me "failed toload external entity"; not quite as descriptive as I'd hoped. :)
Rob Sobers
A: 

I finally narrowed it down to a PHP configuration setting called allow_url_fopen, which was set to Off on the server running the script.

I modified the php.ini file to enable this setting:

allow_url_fopen = On

And now DOMDocument.load can load XML from a remote URL.

WARNING: apparently there are some security issues with keeping this setting on permanently.

Rob Sobers