0

I'm trying to use the Last.fm API for an application I'm creating, but am having some problems with validation.

If an API request gives an error it returns a code and message in the response XML like this:

<lfm status="failed">
<error code="6">No user with that name</error>
</lfm>

However, the request also returns an HTTP status of 400 (or in some cases 403) which DOMDocument considers an error and so then refuses to parse the XML.

Is there any way round this, so that I can retrieve the error code and message?

Thanks

Pete

Pete Williams
  • 221
  • 3
  • 10

4 Answers4

1

A solution could be to separate your manipulations in two steps :

  • First, get the XML string, using curl, for example
  • Then, work on that string with DOMDocument.


There is an example of how you can use curl on the curl_exec manual page ; adding a few useful options, you could use something like this, I suppose :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "YUR_URL_HERE");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml_string = curl_exec($ch);
curl_close($ch);

// You can now work with $xml_string

And, for more options (there are a lot of them ^^ ), you can take a look to the manual page of curl_setopt.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
1

I resolved the issue by using try & catch. If it can help someone

    function getXML($xml) {
            $dom = new DomDocument();
        try {
            @$dom->load($xml); // The '@' is necessary to hide error if it's a error 400 - Bad Request
            $root = $dom->documentElement;
            return $root;
        }
        catch(Exception $e)
        {
            return false;
        }
    }
birdy
  • 11
  • 1
0

You can always get the response with some other function like file_get_contents and then parse the XML with DOMDocument::loadXML

Edit:

http://www.php.net/manual/en/domdocument.load.php#91384

Kemo
  • 6,942
  • 3
  • 32
  • 39
  • This was my first thought too, though sadly it gave the same result: Warning: file_get_contents(someurl.com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in userclass.php on line 23 – Pete Williams Apr 11 '10 at 19:05
0

The Function:

function getAlbum($xml,$artist,$album)
{
  $base_url = $xml;
  $options = array_merge(array(
    'user' => 'YOUR_USERNAME',
    'artist'=>$artist,
    'album'=>$album,
    'period' => NULL,
    'api_key' => 'xYxOxUxRxxAxPxIxxKxExYxx', 
  ));

  $options['method'] = 'album.getinfo';

  // Initialize cURL request and set parameters
  $ch = curl_init($base_url);
  curl_setopt_array($ch, array(
    CURLOPT_URL            => 'http://ws.audioscrobbler.com/2.0/',
    CURLOPT_POST           => TRUE,
    CURLOPT_POSTFIELDS     => $options,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_TIMEOUT        => 30,
    CURLOPT_HTTPHEADER        => array( 'Expect:' ) ,
    CURLOPT_USERAGENT      => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
  ));

  $results = curl_exec($ch);
  unset ($options);
  return $results;
}

Usage:

// Get the XML
$xml_error = getAlbum($xml,$artist,$album);

// Show XML error
if (preg_match("/error/i", $xml_error)) {
    echo " <strong>ERRO:</strong> ".trim(strip_tags($xml_error));
}
Fabiano Shark
  • 384
  • 2
  • 6