简体   繁体   English

创建一个通过last.fm API显示图像的PHP页面

[英]Creating a PHP page that shows an image via last.fm API

I'm trying to do a PHP page to shows an album cover via Last.FM API. 我正在尝试制作一个PHP页面,以通过Last.FM API显示专辑封面。 However, the Artist Name and the Title of the music are provided by a XML file that a software updates via FTP. 但是,艺术家名称和音乐标题由软件通过FTP更新的XML文件提供。

Here is the code of Last.FM api: 这是Last.FM api的代码:

<?php
$img = simplexml_load_file('http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=<APIKEY>&artist=cher&track=believe');
echo '<img src="';
echo $img->track[0]->album[0]->image[3];
echo '">';
?>

Now the link of my XML file is: http://summerblast.pt/avaplayer/rds.xml The info I need to the Last.FM API is in 'OnAir/CurMusic'. 现在,我的XML文件的链接是: http ://summerblast.pt/avaplayer/rds.xml我需要的Last.FM API信息位于“ OnAir / CurMusic”中。

Well, what I am trying to do is change "Cher" and "Believe" (the artist's name and the Title's name) in the link of the "simplexml_load_file" (in the php code) with the info that my XML file provides. 好吧,我想做的是用我的XML文件提供的信息,在“ simplexml_load_file”(在php代码中)的链接中更改“ Cher”和“ Believe”(艺术家的名字和标题的名字)。

Can you please help me doing this? 你能帮我做这个吗?

Thank you all in advance. 谢谢大家。

Sorry was confused to what you were asking from before. 对不起,您对以前的要求感到困惑。 I have gone ahead and corrected to do what you wanted. 我已经走了,改正做你想做的。

You can get the Artist and the Title from rds.xml and just pass it to the URL like the following listed below (note it is important to run urlencode on them since they can have spaces and other things to break the URL): 您可以从rds.xml中获取Artist和Title,并将其传递给URL,如下所示(请注意,对它们运行urlencode非常重要,因为它们可能会有空格和其他东西来破坏URL):

Updated to do error checking 更新以进行错误检查

    $xml = simplexml_load_file('http://summerblast.pt/avaplayer/rds.xml');
    $artist = urlencode($xml->OnAir->CurMusic->Artist);     
    $track =  urlencode($xml->OnAir->CurMusic->Title);
    $url = 'http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=XXXXXXXXXXXXXXXXXXXXXXXXXXX&artist='.$artist.'&track='.$track;
    $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("artist does not have a image"); // do whatever you want to do
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM