从最后获得艺术家图像.FM XML (api artist.getinfo)


Get artist image from last.fm xml (api artist.getinfo)

我想请您帮助我,我有一个xml源(http://livefmhits.6te.net/nowplay.xml),它给了我歌曲的来源,我想通过lastfm (artist.getinfo)在echo中删除封面,我尝试如下:

<?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
    }
}

我无法提取源一定是错误的回声,我喜欢删除说"mega"的图像。我给你完整的链接http://ws.audioscrobbler.com/2.0/?method=artist.getinfo〈ru&artist=COLDPLAY&api_key=ae9dc375e16f12528b329b25a3cca3ee,然而我要做一个帖子你的,但我不能(从最后得到大的艺术家形象。FM XML (api artist.getinfo))

我从一开始就来请求你的帮助,谢谢你的可用性

我在json中是这样做的。在XML中也是一样的

首先,我们定义API KEY:
define('YOUR_API_KEY', 'b25b959554ed76058ac220b7b2e0a026');

最好将它从代码中分离出来,如果您需要在代码的其他地方重用它,这会使事情变得更容易。(如。在另一个函数中)

然后,我们创建了实现奇迹所需的两个函数。

1)要查询Lastfm的API并获取其内容,我们将使用CURL:
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 .'" />';

我想这是不言自明的。;)

希望有帮助!