1
$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>
Rob Sobers
  • 20,737
  • 24
  • 82
  • 111

2 Answers2

4

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.

Community
  • 1
  • 1
Rob Sobers
  • 20,737
  • 24
  • 82
  • 111
2

Add this:

    libxml_use_internal_errors ( true );
    $doc = new DOMDocument();
    $doc -> recover = true;
    $doc -> strictErrorChecking = false;
bcosca
  • 17,371
  • 5
  • 40
  • 51
  • What's this do for me? (I'm not a PHP guy, this is a one-off thing I'm working on) – Rob Sobers Oct 21 '10 at 02:12
  • 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 () );` – bcosca Oct 21 '10 at 02:15
  • 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 Oct 21 '10 at 02:28
  • So, that just gave me "failed to load external entity"; not quite as descriptive as I'd hoped. :) – Rob Sobers Oct 21 '10 at 15:53