PDA

View Full Version : SimpleXML - Problems with external file.



<CrGeary.com/>
29 Jun 2010, 07:05 AM
Does anyone know why this doesn't work?, I am using Last.fm API, so obviously, I am limited, but this shouldn't happen.

I have 2 paths:

http://ws.audioscrobbler.com/2.0/?method=user.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&user=ChrisG4620
http://ws.audioscrobbler.com/2.0/?method=user.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&user=ChrissG4620

URL 1, has a correct username, and URL 2, has an incorrect username, so it would show an error.

So i try running it through SimpleXML in PHP, if i parse url 1. It works fine. If i parse url 2, i get this error:


Warning: SimpleXMLElement::__construct(http://ws.audioscrobbler.com/2.0/?method=user.getInfo&api_key=b25b959554ed76058ac220b7b2e0a026&user=ChrissG4620) [simplexmlelement.--construct]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\EasyPHP5.2.10\www\last.fm\includes\last_fm\test.php on line 3

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: I/O warning : failed to load external entity "http://ws.audioscrobbler.com/2.0/?method=user.getInfo&api_key=b25b959554ed76058ac220b7b2e0a026&user=ChrissG4620" in C:\EasyPHP5.2.10\www\last.fm\includes\last_fm\test.php on line 3

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\EasyPHP5.2.10\www\last.fm\includes\last_fm\test.php:3 Stack trace: #0 C:\EasyPHP5.2.10\www\last.fm\includes\last_fm\test.php(3): SimpleXMLElement->__construct('http://ws.audio...', 0, true) #1 {main} thrown in C:\EasyPHP5.2.10\www\last.fm\includes\last_fm\test.php on line 3

--
This is the PHP (with the wrong username)

$result = new SimpleXMLElement( 'http://ws.audioscrobbler.com/2.0/?method=user.getInfo&api_key=cf6d5284dbbc825d5472c077c30b22ae&user=ChrissG4620', null, TRUE );

print_r($result);
---
Anyone got any ideas why?, If you send an incorrect username, it shows an error in XML form, so how come the SimpleXML doesn't treat it like a normal request?

Alan
29 Jun 2010, 05:17 PM
Was actually curious myself, so had a look at the headers. A 400 status code is being returned by the server (as mentioned in the error message). Problem solved.