【问题标题】:Get artist image from last.fm xml (api artist.getinfo)从 last.fm xml (api artist.getinfo) 获取艺术家图像
【发布时间】:2015-11-14 20:26:23
【问题描述】:

我想请你帮忙我有一个 xml 源 (http://livefmhits.6te.net/nowplay.xml),它给了我歌曲的来源,我想通过 echo 中的 lastfm (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
    }
}

我无法提取来源一定是错误的回声,我喜欢删除显示“mega”的图像。我给你完整的链接 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))

我从一开始就来寻求您对这项工作的帮助,感谢您有空

【问题讨论】:

    标签: php xml api last.fm


    【解决方案1】:

    这是我在 json 中的做法。在 XML 中几乎是一样的。

    首先,我们定义 API KEY:

    define('YOUR_API_KEY', 'b25b959554ed76058ac220b7b2e0a026');
    

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

    然后,我们创建实现魔法所需的 2 个函数。

    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 .'" />';
    

    我认为这里是不言自明的。 ;)

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多