简体   繁体   中英

Display Artist Info from Last.FM API

Having recently had some problems with my code which calls for artist data fed from Last.FM API, I'm looking for a different solution but am struggling to find a nice easy way to handle the problem. Currently I have:

    <?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; ?> 

Obviously, this is a problem for security as I don't want to enable fopen. Can anyone point me in the direction of a solution? Most people suggest cURL but I have no clue about using it so would be grateful for some help there.

cURL is a PHP library. Here are some links that explain it well:

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

For others who may read this: If your hosting company doesn't have fopen enabled, you may be able to create a "local" php.ini file in your web root directory with this line:

allow_url_fopen = ON

You called simplexml_load_file twice. Try:

<?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 version:

<?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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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