1

Currently, I am attempting to use the metadata from our streaming provider, StreamOn to send a request out to Last.FM to get the original sized album artwork. I am new to the world of APIs so it is rather confusing to me, however I am managing. My problem arises in the sending and receiving of the image over XML. In the code below, the contents of the metadata page are set as variables, which are then A.) displayed, and B.) used to look for the appropriate album artwork.

<?php

$request = file_get_contents('http://ckpk.streamon.fm/card.php');
$doubleQuotation = '"';

//Artist Request

$artistTitle = '"artist": "';
$artistTitlePosition = intval(strpos($request, $artistTitle));
$artistBeginningPosition = $artistTitlePosition + 11;
$artistEndingPosition = intval(strpos($request, $doubleQuotation, $artistBeginningPosition));
$artistName = substr($request, $artistBeginningPosition, $artistEndingPosition - $artistBeginningPosition);
echo '<b>' . $artistName . '</b>';
echo '<br />';
$artist = $artistName;


//Track Request

$trackTitle = '"title": "';
$trackTitlePosition = intval(strpos($request, $trackTitle));
$trackBeginningPosition = $trackTitlePosition + 10;
$trackEndingPosition = intval(strpos($request, $doubleQuotation, $trackBeginningPosition));
$trackName = substr($request, $trackBeginningPosition, $trackEndingPosition - $trackBeginningPosition);
echo '<i>' . $trackName . '</i>';
echo '<br />';


//Album Name Request

$albumTitle = '"album": "';
$albumTitlePosition = intval(strpos($request, $albumTitle));
$albumBeginningPosition = $albumTitlePosition + 10;
$albumEndingPosition = intval(strpos($request, $doubleQuotation, $albumBeginningPosition));
$albumName = substr($request, $albumBeginningPosition, $albumEndingPosition - $albumBeginningPosition);
echo $albumName;
$album = $albumName;



/*
* Last.FM Artwork Class
* Author: Caleb Mingle (@dentafrice)
* http://dentafrice.com
*/

class LastFM {
    const API_KEY = "7facb82a2a573dd483d931044030e30c";
    public static $size_map = array("small" => 0, "medium" => 1, "large" => 2, "extralarge" => 3, "mega" => 4);
    public static function getArtwork($artist, $return_image = false, $size = "image_mega", $album) {
        $artist = urlencode($artist);
        $returnedInfo    = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&api_key=" . self::API_KEY . "&artist=" . $artist . "&album=" . $album . "&image=" . self::$size_map[$size] . "&format=json";
        $returnedInfo    = @file_get_contents($returnedInfo);

        if(!$returnedInfo) {
            return;  // Artist lookup failed.
        }

        $xml = new SimpleXMLElement($xml);
        $xml = $xml->artist;
        $xml = $xml->image[self::$size_map[$size]];

        return (!$return_image) ? $xml : '<img src="' . $xml . '" alt="' . urldecode($artist) . '" />';
    }
}


$artwork = LastFM::getArtwork($artist, true, $size, $album);


if($artwork) {
echo $artwork;
}
else{
return;
}

?>

I temporarily styled the elements to distinguish between them and I will worry about the styling later. However, I would like to know how to go about using the data to send a request to the Lsat.FM servers and receive the image to then properly display it. It's different with StreamOn than with something else, such as ShoutCast.

edwardmp
  • 6,339
  • 5
  • 50
  • 77
montelc0
  • 101
  • 9
  • What is your exact question? Can't figure it out at this point.. By the way, the format is specified as JSON while it assumes an XML object.. `self::$size_map[$size] . "&format=json";` – edwardmp Jul 08 '13 at 18:09
  • Could that be where the problem is? My question was regarding how to get the XML image to load because I thought I was sending it out to Last.FM correctly. What I have to do is use the meta data and insert it into that URL via concatenation. When you go to the JSON file, it hsa the link to the original size image. I need this image, but I don't k now how to get there yet. This is the page that I've used for testing: http://wpovfm.org/moldycheese/asdf.php – montelc0 Jul 08 '13 at 18:23

1 Answers1

0

According to your reply in the comments, my hypothesis is confirmed.

Because you are requesting the Last.FM API to return JSON, you get the error:

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML

Because it is JSON, not XML.

You can fix this in two ways. Either change the response format to XML or do not use SimpleXML but json_decode() instead.

The first is probably the easiest to adjust. Try to change the &format=json part to &format=xml

I haven't tested this but highly probably that this will be supported by Last.fm.

Also note that you don't supply an image size, and "image_mega" the default value is not a valid value (see API specs @ http://www.last.fm/api/show/artist.getInfo). You probably want want to use "mega" not "image_mega"

Updated, working code using JSON:

<?php

class LastFM {
    const API_KEY = "7facb82a2a573dd483d931044030e30c";
    public static $size_map = array("small" => 0, "medium" => 1, "large" => 2, "extralarge" => 3, "mega" => 4);

    public static function getArtwork($artist, $return_image = false, $size = "mega", $album) {
        $artist = urlencode($artist);
        $album = urlencode($album);

        $returnedInfo = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&api_key=" . self::API_KEY . "&artist=" . $artist . "&album=" . $album . "&image=" . self::$size_map[$size] . "&format=json";
        $returnedInfo = @file_get_contents($returnedInfo);

        if(!$returnedInfo) {
            return;  // Artist lookup failed.
        }

        $json = json_decode($returnedInfo, true);
        $albumArt = $json["artist"]["image"][self::$size_map[$size]]["#text"];

        return (!$return_image) ? print_r($json) : '<img src="' . $albumArt . '" alt="' . urldecode($artist) . '" />';
    }
}

$artist = "Daft Punk";
$album = "Random Access Memories";
$size = "mega";

$artwork = LastFM::getArtwork($artist, true, $size, $album);

if($artwork) {
    echo $artwork;
}
else{
    return;
}

I know I rock. I choose JSON because I find it easier to work with. It returns exactly what you want, e.g.:

<img src="http://userserve-ak.last.fm/serve/500/10923145/Daft+Punk+6a00e398210d13883300e55ui6.png" alt="Daft Punk" />

Finally, I added urlencoding() for the album name, since I had issues with that in my example.

Working PHPFiddle

edwardmp
  • 6,339
  • 5
  • 50
  • 77
  • http://ws.audioscrobbler.com/2.0/?method=album.getinfo&artist=Chris+Tomlin%20&album=Burning+Lights&api_key=7facb82a2a573dd483d931044030e30c I tried this out to see what the XML would look like. I am far closer to a final solution, however, I am not sure how to select only the mega image. If you look at the above link, it doesn't appear that they are listed as an array. I need that mega image since I have to scale it responsively in the implementation of the actual site's player. Thanks in advance for your continued assistance. – montelc0 Jul 08 '13 at 22:06
  • OK, because I rock I've tested it out and modified it a bit. I choose JSON because I find it easier to work with. Check my edited answer :) – edwardmp Jul 08 '13 at 22:45
  • You seriously rock. 10/10. – montelc0 Jul 08 '13 at 22:52
  • Now would it also work to get the RAM album artwork or would that be something else? – montelc0 Jul 08 '13 at 22:53
  • I'm not sure, I'm not familiar with the Last.fm API. But what is this image used for? There appear no other images other than sizes versions in the API response – edwardmp Jul 08 '13 at 22:56
  • Strike that. I got it. I changed the method to album.getinfo and the `$json` to ["album"] rather than ["artist"]. I'm going to credit you in the acknowledgements of the site. – montelc0 Jul 08 '13 at 22:58
  • :) You're welcome. Send me a link at my username here at gmail.com – edwardmp Jul 08 '13 at 22:59