从last.fm获取艺术家图像xml(api artist.getinfo)

从last.fm获取艺术家图像xml(api artist.getinfo)

问题描述:

我想问你的帮助我有一个xml源(http://livefmhits.6te.net/nowplay.xml)它给了我这首歌的来源,我想通过lastfm去掉封面( artist.getinfo)回声我试过如下:从last.fm获取艺术家图像xml(api artist.getinfo)

<?php 
$xml = simplexml_load_file('http://livefmhits.6te.net/nowplay.xml'); 
$artist = urlencode($xml->TRACK["ARTIST"]);  
$url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist='.$artist.&api_key=b25b959554ed76058ac220b7b2e0a026; 
$xml2 = @simplexml_load_file($url); 
if ($xml2 === false) 
{ 
    echo("Url failed"); // do whatever you want to do 
} 
else 
{ 
    if($xml2->track->album->image[3]) 
    { 
     echo '<img src="'; 
     echo((string) $xml2->track->album->image[3]);  
     echo '">'; 
    } 
    else 
    { 
     echo "<img src='http://3.bp.blogspot.com/-SEsYAbASI68/VZ7xNuKy-GI/AAAAAAAAA3M/IWcGRDoXXms/s1600/capaindisponivel.png'"; // do whatever you want to do 
    } 
} 

我不能够提取源必须是错的回声,我想删除,上面写着“巨型”的形象。我向你完整的链接 http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&lang=ru&artist=COLDPLAY&api_key=ae9dc375e16f12528b329b25a3cca3ee,但我做了后你的,但我不能(Get large artist image from last.fm xml (api artist.getinfo)

我来问你的帮助,这项工作从一开始就感谢可用性

这里我如何在json中做这件事。这在XML中几乎是一样的。

首先,我们定义的API KEY:

define('YOUR_API_KEY', 'b25b959554ed76058ac220b7b2e0a026'); 

最好是它从代码中分离出来,这让事情变得更简单,如果你需要在你的代码在其他地方重复使用它。 (例如用于其他功能)

然后,我们创建了2个我们需要使魔法发生的功能。

1)要查询LastFM等的API,并得到它的内容,我们将使用卷曲:

function _curl($url) 
    { 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); 

     if(strtolower(parse_url($url, PHP_URL_SCHEME)) == 'https') 
     { 
      curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1); 
      curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,1); 
     } 

     curl_setopt($ch, CURLOPT_URL, $url); 
     $data = curl_exec($ch); 
     curl_close($ch); 

     return $data; 
    } 

2)LastFM等提供了许多选择。就我个人而言,我发现将主查询分解成函数更容易。但是,因为你只要目标图像,这里是功能我会用:

function lfm_img($artist) 
{ 
    $url = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=$artist&api_key=".YOUR_API_KEY."&format=json";    
    $json = _cul($url); 

    $data = str_ireplace("#text", "text", $json); 
    $list = json_decode($data); 
    //If an error occurs... 
    if($list->error) 
     return 'ERROR.'. $list->error;  
    //That's where we get the photo. We try to get the biggest size first, if not we try smaller sizes. Returns '0' if nothing is found. 
    if($list->artist->image[4]) 
     $img = $list->artist->image[4]->text; 
    else if($list->artist->image[3]) 
     $img = $list->artist->image[3]; 
    else if($list->artist->image[2]) 
     $img = $list->artist->image[2]; 
    else if($list->artist->image[1]) 
     $img = $list->artist->image[1]; 
    else if($list->artist->image[0]) 
     $img = $list->artist->image[0]; 
    else 
     $img = 0; 

    return $img; 
} 

最后,使用它们:

$artist_query = 'Nirvana'; 
$artist_image = lfm_img($artist); 
//display image 
echo '<img src="'. $artist_image .'" alt="'. $artist_query .'" />'; 

我认为这是自我解释在这里。 ;)

希望它有帮助!

+0

SuN,这个工作没事,谢谢! –