Php 从last.fm xml(api artist.getinfo)获取艺术家图像
我想请你帮忙我有一个xml源()它给了我这首歌的源,我想通过echo中的lastfm(artist.getinfo)删除封面,我尝试了以下方法:Php 从last.fm xml(api artist.getinfo)获取艺术家图像,php,xml,api,last.fm,Php,Xml,Api,Last.fm,我想请你帮忙我有一个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/?metho
<?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
}
}
曲目[“艺术家”];
$url='1http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=“.$artist&api_key=b25b959554ed76058ac220b7b2e0a026;
$xml2=@simplexml\u load\u文件($url);
如果($xml2==false)
{
echo(“Url失败”);//做任何你想做的事
}
其他的
{
如果($xml2->track->album->image[3])
{
echo'曲目->专辑->图像[3]);
回声“>”;
}
其他的
{
回声“
我无法提取的来源必须是错误的回声,我想删除的图像,说“兆头”。我现在给你完整的链接
但我本来要写你的帖子,但我不能()
在这项工作中,我从一开始就请求您的帮助,谢谢您的帮助。以下是我在json中的工作方式。在XML中基本相同
首先,我们定义API密钥:
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 .'" />';
$artist_query='Nirvana';
$artist\u image=lfm\u img($artist);
//显示图像
回声';
我认为这是不言自明的
希望有帮助