目前,我正在尝试使用来自我们的流媒体提供商 StreamOn 的元数据向 Last.FM 发送请求以获取原始大小的专辑插图。我是 API 世界的新手,所以这让我很困惑,但我正在管理。我的问题出现在通过 XML 发送和接收图像的过程中。在下面的代码中,元数据页面的内容被设置为变量,然后 A.) 显示,B.) 用于查找适当的专辑插图。
<?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;
}
?>
我暂时设置了元素的样式以区分它们,稍后我会担心样式。但是,我想知道如何使用数据向 Lsat.FM 服务器发送请求并接收图像以正确显示它。StreamOn 与其他东西(例如 ShoutCast)不同。