所以我连接到最后一个fm API,信息看起来像这个
{"topartists":{"artist":[{"name":"Bee Gees","mbid":"bf0f7e29-dfe1-416c-b5c6-f9ebc19ea810","url":"https://www.last.fm/music/Bee+Gees","streamable":"0","image":[{"#text":"https://lastfm-img2.akamaized.net/i/u/34s/1df682d5ba6e45db843c45a336170470.png","size":"small"},{"#text":"https://lastfm-img2.akamaized.net/i/u/64s/1df682d5ba6e45db843c45a336170470.png","size":"medium"},{"#text":"https://lastfm-img2.akamaized.net/i/u/174s/1df682d5ba6e45db843c45a336170470.png","size":"large"},{"#text":"https://lastfm-img2.akamaized.net/i/u/300x300/1df682d5ba6e45db843c45a336170470.png","size":"extralarge"},{"#text":"https://lastfm-img2.akamaized.net/i/u/300x300/1df682d5ba6e45db843c45a336170470.png","size":"mega"}],"@attr":{"rank":"1"}}
在我的PHP代码中,我使用
class LastFm{
private $api_key;
const url = 'http://ws.audioscrobbler.com/2.0/';
function __construct($api_key){
$this->api_key = $api_key;
}
function call($method,$params=array()){
$lastfm = self::url.'?method='.$method.'&format=json&api_key='.$this->api_key;
foreach($params as $key => $value){
$lastfm .= '&'.$key.'='.urlencode($value);
}
$json = file_get_contents($lastfm);
return json_decode($json, true);
}
}
?>
<?php
$lastfm = new LastFm('63692beaaf8ba794a541bca291234cd3');
$tracks = $lastfm->call('tag.gettopartists&tag=disco');
foreach($tracks['topartists']['artist'] as $track){
?>
成功返回艺术家姓名。但是我如何获得中等大小的图像?我试过了 -
`echo $artist['image'];`
但没有任何回报。
答案 0 :(得分:1)
echo '<img src="'.$track['image'][0]['#text'].'">';
是您正在寻找的。 p>
0
是$track['image']
数组中的索引,您可能希望根据所需的图像大小进行更改...或者,您可以循环它并填充新的图像数组
$images = [];
foreach($track['image'] as $image){
$images[] = [ $image['size']=>$image['#text'] ];
}
echo '<img src="'.$images['small'].'"/>;