major_domo Posted June 26, 2006 Share Posted June 26, 2006 [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 122Warning: 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 alreadyAny one encounter this before? Quote Link to comment Share on other sites More sharing options...
major_domo Posted June 26, 2006 Author Share Posted June 26, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.