从Last.FM API显示艺术家信息

时间:2011-12-07 23:35:13

标签: php xml parsing curl last.fm

最近我的代码出现了一些问题,需要从Last.FM API提供艺术家数据,我正在寻找一个不同的解决方案,但我很难找到一个很好的简单方法来处理这个问题。目前我有:

    <?php $feed = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=anartistname&api_key=01234567890");
$xml = simplexml_load_file($feed);
$info = $xml->artist->bio->summary; ?>

<?php echo $info; ?> 

显然,这是一个安全问题,因为我不想启用fopen。有人能指出我的解决方案吗?大多数人都建议使用cURL,但我对使用它没有任何线索,所以会感激一些帮助。

2 个答案:

答案 0 :(得分:0)

cURL是一个PHP库。以下是一些可以解释它的链接:

http://themekraft.com/getting-json-data-with-php-curl/ http://www.jonasjohn.de/snippets/php/curl-example.htm

对于其他可能阅读此内容的人:如果您的托管公司未启用fopen,您可以使用此行在您的网络根目录中创建“本地”php.ini文件:

allow_url_fopen = ON

答案 1 :(得分:0)

您曾两次致电simplexml_load_file。 尝试:

<?php

$feed = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=anartistname&api_key=YOUR_KEY';
$xml = simplexml_load_file($feed);
$info = $xml->artist->bio->summary;

echo $info;

cURL版本:

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=anartistname&api_key=YOUR_KEY');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$xml = curl_exec($ch);
curl_close($ch);

$xml = simplexml_load_string($xml);