Jump to content


Photo

simplexml_load_file() = http request failed! 400 warning


  • Please log in to reply
1 reply to this topic

#1 major_domo

major_domo
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 June 2006 - 06:22 AM

My problem:
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)

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

Results:
[!--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?

#2 major_domo

major_domo
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 June 2006 - 04:55 PM

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:
$getstring = '?bar=words with spaces';
$action  = 'http://foo.com/12345/file.xml' . $getstring;
$xml = simplexml_load_file( $action );

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:
$getstring = urlencode( '?bar=words with spaces' );
$action  = 'http://foo.com/12345/file.xml' . $getstring;
$xml = simplexml_load_file( $action );

Problem solved. I hope this helps someone else.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users