1

I'm using simplexml_load_file to pull album information from the LastFM API and having no problems when the requested album matches.

However, when the album is not found, LastFM returns an error, which causes the code below to output a "failed to open stream" error.

I can see that LastFM is giving me exactly what I need, but am unsure how to proceed. What is the proper way to update the code so that this error/error code is correctly handled?

Code:

$feed = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=".$apikey."&artist=".$artist."&album=".$album."&autocorrect=".$autocorrect);

$albums = $feed->album;
foreach($albums as $album) {

    $name = $album->name;
    $img = $album->children();
    $img_big = $img->image[4];
    $img_small = $img->image[2];
    $releasedate = $album->releasedate;
    $newdate = date("F j, Y", strtotime($releasedate));

    if ($img == "") {
            $img = $emptyart; }
}
?>
LowVelocity
  • 65
  • 1
  • 6

3 Answers3

1
   if ($headers[0] == 'HTTP/1.0 400 Bad Request') {
      $img_big = $emptyart;
      $img_small = $emptyart;
   }

That will break with a 403 error ...

Method 1 (not practical): Basically you can mute the error with @ and check if your $feed is empty. In that case, "some error" has happened, either an error with your url or a status failed from last.fm.

    $feed = @simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=".$apikey."&artist=".$artist."&album=".$album."&autocorrect=".$autocorrect);
    if ($feed)
    {
        // fetch the album info
    }
    else
    {
        echo "No results found.";
    }

In both ways, you get no results so you can probably satisfy your needs and showing "No results found" instead of showing him "Last.fm error status code, etc.."

Method 2 (recommended) - Use curl:

    function load_file_from_url($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_REFERER, 'http://www.test.com/');
        $str = curl_exec($curl);

        if(str === false)
        {
            echo 'Error loading feed, please try again later.';
        }

        curl_close($curl);
        return $str;
    }

    function load_xml_from_url($url) {
        return @simplexml_load_string(load_file_from_url($url));
    }

    function getFeed($feedURL)
    {
        $feed = load_xml_from_url($feedURL);
        if ($feed)
        {
                if ($feed['status'] == "failed")
            {
                echo "FAIL";
            }
            else
            {
                echo "WIN";
            }
        }
        else {echo "Last.fm error";}
    }

    /* Example:
       Album: Heritage
       Artist: Opeth
    */

    $url = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=273f3dc84c2d55501f3dc25a3d61eb39&artist=opeth&album=hwwXCvuiweitage&autocorrect=0";
    $feed = getFeed ($url);
    // will echo FAIL

    $url2 = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=273f3dc84c2d55501f3dc25a3d61eb39&artist=opeth&album=heritage&autocorrect=0";
    $feed2 = getFeed ($url2);
    // will echo WIN
P-S
  • 3,876
  • 1
  • 29
  • 26
0

Wouldn't it be better to first get the url, check if it doesn't contain the error, and then use simplexml_load_string instead?

XIU
  • 804
  • 7
  • 11
  • I tried something along perhaps-similar lines, using file_get_contents and an If statement, but only received a different error. How would you get and check the URL? – LowVelocity Oct 21 '12 at 00:41
0

Working along the lines of evaluating the state of the URL prior to passing it to simplexml_load_file, I tried get_headers, and that, coupled with an if/else statement, seems to have gotten things working.

$url = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=".$apikey."&artist=".$artist."&album=".$album."&autocorrect=".$autocorrect;

$headers = get_headers($url, 1);

if ($headers[0] == 'HTTP/1.0 400 Bad Request') {
    $img_big = $emptyart;
    $img_small = $emptyart;
    }

else {
    $feed = simplexml_load_file($url);

    $albums = $feed->album;
    foreach($albums as $album) {

        $name = $album->name;
        $img = $album->children();
        $img_big = $img->image[4];
        $img_small = $img->image[2];
        $releasedate = $album->releasedate;
        $newdate = date("F j, Y", strtotime($releasedate));

        if ($img == "") {
            $img_big = $emptyart;
            $img_small = $emptyart;
            }
    }
}
LowVelocity
  • 65
  • 1
  • 6