Home > PHP, Programming > Adding a Last.fm bio to your website

Adding a Last.fm bio to your website

On this article we’re going to create a (simple) script in order to retrieve an artist bio from last.fm and create somehow visually pleasant style to present it to our site…

sample

what we’re going to make

First of all, we need to go to the Last.fm site in order to obtain an API key. If you read the documentation you’ll see that our request can be made by using REST of XML-RPC requests. In this tutorial we’re going use the first way.

Making the request

We’re going to make a POST request using curl.

$ch = curl_init();
$artist = strtolower($_GET["artist"]);
$fields = array('method' => 'artist.getInfo',
'artist' => $artist,
'api_key' => 'your_api_key');
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, 'http://ws.audioscrobbler.com/2.0/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

Handling the response

We load the response using SimpleXML

$s = simplexml_load_string($response);

Getting the artist's bio from the response

print $s->artist->image[3];

Getting the similar artists and the tags

foreach($s->artist->similar->artist AS $similar)
{
    print $similar->name;
}
foreach($s->artist->tags->tag AS $tag)
{
    print $tag->name;
}

Putting it all together

Adding some style

Categories: PHP, Programming Tags: ,
  1. Torbjorn Ljung
    May 5th, 2014 at 00:29 | #1

    Just tried your complete example, but I get error=6=no artist or id submitted. The fields_string upon request = “method=artist.getInfo&Bob Dylan=&api_key=[api_key]&”.
    Do you know whats wrong ?
    Kind Regards
    Torbjorn Ljung
    Sweden

    • Vartan Aivazian
      May 6th, 2014 at 00:17 | #2

      You are missing a param… it should be method=artist.getInfo&artist=Bob Dylan&api_key=[your_key]

      • Torbjorn Ljung
        May 8th, 2014 at 16:46 | #3

        Hi Vartan, thanks for your quick response.
        Could you pls correct this code:
        $artist = strtolower($_GET[“artist”]);
        $fields = array(‘method’ => ‘artist.getInfo’,
        ‘Bob Dylan’ => $artist,
        ‘api_key’ => ’06a57376a1afdeb4cea090982a47fdcf’);

        foreach($fields as $key=>$value) { $fields_string .= $key.’=’.$value.’&’; }
        rtrim($fields_string,’&’);
        Cheers, Torbjorn

        • Vartan Aivazian
          May 9th, 2014 at 20:02 | #4

          $fields = array(‘method’ => ‘artist.getInfo’,
          ‘artist’ => ‘Bob Dylan’,
          ‘api_key’ => ’06a57376a1afdeb4cea090982a47fdcf’);

  2. April 17th, 2015 at 21:54 | #5

    Hi, man please help me, indefined $fields_string.
    As you can see below:
    http://prntscr.com/6uyjcs

  3. April 17th, 2015 at 21:57 | #6

    Hi, man please help me, indefined $fields_string.
    As you can see below:
    http://prntscr.com/6uyjcs

    [CODE]

    ‘artist.getInfo’,
    ‘artist’ => $artist,
    ‘api_key’ => ‘i took out for security reasons’);

    foreach($fields as $key=>$value) { $fields_string .= $key.’=’.$value.’&’; }
    rtrim($fields_string,’&’);

    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_URL, ‘http://ws.audioscrobbler.com/2.0/’);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_USERAGENT, “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);
    $s = simplexml_load_string($response);

    ?>

    <img src="artist->image[3]; ?>” alt=”” />
    artist->bio->content)); ?>

    Tags:

    artist->tags->tag AS $tag)
    {?>
    name;?>

    Similar Artists:

    artist->similar->artist AS $similar)
    {?>
    name; ?>

    [CODE]

  4. April 17th, 2015 at 22:00 | #7

    The code was incomplete, sorry:/

    [CODE]

    ‘artist.getInfo’,
    ‘artist’ => $artist,
    ‘api_key’ => ‘i took out for security reasons8′);

    foreach($fields as $key=>$value) { $fields_string .= $key.’=’.$value.’&’; }
    rtrim($fields_string,’&’);

    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_URL, ‘http://ws.audioscrobbler.com/2.0/’);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_USERAGENT, “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);
    $s = simplexml_load_string($response);

    ?>

    <img src="artist->image[3]; ?>” alt=”” />
    artist->bio->content)); ?>

    Tags:

    artist->tags->tag AS $tag)
    {?>
    name;?>

    Similar Artists:

    artist->similar->artist AS $similar)
    {?>
    name; ?>

    [CODE]

  5. April 17th, 2015 at 22:03 | #8

    It must be the number of caracteres:

    ‘artist.getInfo’,
    ‘artist’ => $artist,
    ‘api_key’ => ‘i took out for security reasons8′);

    foreach($fields as $key=>$value) { $fields_string .= $key.’=’.$value.’&’; }
    rtrim($fields_string,’&’);

    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_URL, ‘http://ws.audioscrobbler.com/2.0/’);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_USERAGENT, “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);
    $s = simplexml_load_string($response);

    ?>

  6. Vartan Aivazian
    April 18th, 2015 at 17:56 | #9

    @El Jonhnny

    It’s because the variable doesn’t have an initial value… it shouldn’t be critical though. Just add

    $fields_string = ”;

    on the line before foreach

  1. No trackbacks yet.

%d bloggers like this: