代码之家  ›  专栏  ›  技术社区  ›  André Santos

打开LastFM的歌手图像和功能(artist.getinfo)

  •  2
  • André Santos  · 技术社区  · 9 年前

    很好,我有LastFM API的代码

    <?php
    $xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=ARIANA GRANDE&api_key=b25b959554ed76058ac220b7b2e0a026");
    $largeImage = $xml->xpath('/lfm/artist/image[@size="mega"]')[0];
    echo '<img src="'.$largeImage.'" />';     
    ?>

    它似乎使用了Ariana Grande的图片作为php页面。

    现在我的XML文件的链接是: http://radiojoven.6te.net/playlist.xml

    好吧,我想做的是用我的XML文件提供的信息更改“simplexml_load_file”(在php代码中)链接中的“ARIANA GRANDE”(艺术家的名字)。我试图编写这样的代码,但没有成功。

    <?php
    $xml = simplexml_load_file('http://radiojoven.6te.net/playlist.xml');
    $artist = urlencode($xml->Event->Song->Artist['name'];); 
    $url = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist='.$artist.'&api_key=50ac27433c63f7298064f434f4ef6d15);
    $xml2 = @simplexml_load_file($url);
    $largeImage = $xml2->xpath('/lfm/artist/image[@size="mega"]')[0];
    echo '<img src="'.$largeImage.'" />';     
    ?>
    

    你能帮我做这件事吗?

    提前感谢大家。

    1 回复  |  直到 9 年前
        1
  •  1
  •   The fourth bird    9 年前

    我认为您的代码中有一些错误。

    删除后面的分号 ['name']; 在这行中:

    $artist = urlencode($xml->Event->Song->Artist['name'];);
    

    后面缺少双引号 api_key=50ac27433c63f7298064f434f4ef6d15 我想 '.$artist.' 应该是 $artist 在这行中:

    $url = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist='.$artist.'&api_key=50ac27433c63f7298064f434f4ef6d15);
    

    我认为你不需要这条线:

    $xml2 = @simplexml_load_file($url);
    

    然后在这行中更改 $xml2 $url :

    $largeImage = $xml2->xpath('/lfm/artist/image[@size="mega"]')[0];
    

    所以你的代码会变成这样:

    <?php
    $xml = simplexml_load_file('http://radiojoven.6te.net/playlist.xml');
    $artist = urlencode($xml->Event->Song->Artist['name']);
    $url = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=$artist&api_key=50ac27433c63f7298064f434f4ef6d15");
    $largeImage = $url->xpath('/lfm/artist/image[@size="mega"]')[0];
    echo '<img src="'.$largeImage.'" />';     
    ?>