Jump to content

Archived

This topic is now archived and is closed to further replies.

major_domo

simplexml_load_file() = http request failed! 400 warning

Recommended Posts

[b]My problem:[/b]
I'm having problems loading XML data from remote server. I don't think I can give away the exact XML url, because of the paperwork behind it, but suffice it to know that all this code works PERFECTLY on my workstation's development environment (Win 2k, Apache 1.3.x, PHP 5.1.2). I just can't get it to work on the live server (CentOS Linux, Apache 2.x, PHP 5.1.2)

[b]Code:[/b]
[code]$importantNumber = '12345';
$action  = 'http://foo.com/' . $importantNumber . '/file.xml';,
$xml = simplexml_load_file( $action );
[/code]

[b]Results:[/b]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: simplexml_load_file(http://foo.com/12345/file.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /var/www/project/compare.inc on line 122

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http%3A//foo.com/12345/file.xml" in /var/www/project/compare.inc on line 122[/quote]

In summary, the URL gets esccaped as 'http%3A//...', which results in the 400 error.

And, yes, allow_url_fopen = On, and yes, I RTFM already

Any one encounter this before?

Share this post


Link to post
Share on other sites
Well, I figgured this out...and I'm answering my own question for anyone else searching for this problem.

The example url I wrote didn't say everything there was to this. Here's a better example of what's happening:[code]$getstring = '?bar=words with spaces';
$action  = 'http://foo.com/12345/file.xml' . $getstring;
$xml = simplexml_load_file( $action );[/code]

Now obviously the spaces in $getstring need to be urlencode()ed, but newer versions of simplexml_load_file() handle that automatically, so the server response was coming back with the proper %20 in place of spaces:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http%3A//foo.com/12345/file.xml?bar=words%20with%20spaces" in /var/www/project/compare.inc on line 122[/quote]
The problem is that the automatic urlencode() was also encoding the rest of $action, including the "http://" part.

The new, working PHP looks like this:[code]$getstring = urlencode( '?bar=words with spaces' );
$action  = 'http://foo.com/12345/file.xml' . $getstring;
$xml = simplexml_load_file( $action );[/code]

Problem solved. I hope this helps someone else.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.