Skip to content

Instantly share code, notes, and snippets.

@mloberg
Created April 18, 2011 20:40
Show Gist options
  • Save mloberg/926122 to your computer and use it in GitHub Desktop.
Save mloberg/926122 to your computer and use it in GitHub Desktop.
Last.fm Simple PHP Class
<?php
include_once('lastfm.php');
$lastfm = new LastFm('your-api-key');
$params = array(
'user' => 'mloberg'
);
print_r($lastfm->call('user.getInfo', $params));
$tracks = $lastfm->call('user.getRecentTracks', $params);
foreach($tracks['recenttracks']['track'] as $track){
echo $track['name'].' by '.$track['artist']['#text'].'<br />';
}
<?php
/**
* This simple last.fm php class was written by Matthew Loberg (http://mloberg.com)
*/
class LastFm{
private $api_key;
const url = 'http://ws.audioscrobbler.com/2.0/';
function __construct($api_key){
$this->api_key = $api_key;
}
function call($method,$params=array()){
$lastfm = self::url.'?method='.$method.'&format=json&api_key='.$this->api_key;
foreach($params as $key => $value){
$lastfm .= '&'.$key.'='.urlencode($value);
}
$json = file_get_contents($lastfm);
return json_decode($json, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment