質問

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.

役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top