Last.fm widget in php : show recently played songs on a website.
Inspired by an idea of the site mxmm.de and wanting to change the default last.fm widget in Flash to use something more flexible, I made one ad hoc.
E'possibile use the JSON output of the last songs played, using the Last.fm API, read, and place as you want.
The only things to have set your username and your API key, you can also change the CSS to suit your tastes.
With this code, I have included in homepage widget a wider and less high, compared to that in Flash of last.fm
Also, unlike the widget Journal of last.fm, This indicates how many hours and minutes have gone from listening to that song (he entered 24 hours).
Hovering the mouse cursor over a song and waiting a couple of seconds, will show the time and date it was played that song (works with both Internet Explorer and Firefox)

Example of a widget to show the songs from last.fm
Here is the code:
(update 10 April 2010: Added the function “NOW”, if you're listening to the song at that moment)
(update in June 2012: Clean code and optimized, and added the function which provides that the site is down for Last.fm)
(update in February 2013: Step by API v1 to v2 Last.fm API, with the addition of a cache file)
<head> <style> #lastfm-title { width: 500px; background-color: #78a5d3; color: #dce8f4; font-size: 19px; font-weight: bold; padding-left: 5px; } .lastfm-homepage { width: 500px; text-align:left; background-color: #dee9ed; color: #000000; padding-left: 5px; } .lastfm-homepage a { text-decoration: none; color: #267dcd; } .lastfm-homepage a:hover { text-decoration: none; color: #00ffff; } </style> <meta content="text/html;charset=utf-8" http-equiv="Content-Type" /> </head> <? /* Last.fm PHP widget. It shows the last $songs played songs. You just need to insert your username, name of the cachefile, expiration time of the cachefile, and your API key. Source: https://www.flapane.com/blog/2009/03/lastfm-widget-in-php-mostrare-le-canzoni-ascoltate-di-recente-in-un-sito-web/ (C) flapane.com - Sep-2014*/ $username = "your username"; //Last.fm Username $expiretime=2; // cache expire time in minutes $apikey="YOUR API KEY"; //your api key $cachename="lastfm.cache"; //name of the cachefile $songs=4; //how many songs to show //create the cachefile if it doesn't exist if (!file_exists($cacheName)) { $create = fopen($cacheName, 'W'); chmod ("$cacheName", 0644); //set chmod 644 fclose($create); } // Is the file older than $expiretime, or the file is new/empty? $FileAge = time() - filectime($cacheName); // Calculate file age in seconds if ($FileAge > ($expiretime * 60) || 0 == filesize($cacheName)) { $url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=$username&api_key=$apikey&limit=$songs&extended=0&format=json"; $lastfm_json = file_get_contents($url); $handle = fopen($cacheName, 'wb'); fwrite($handle, $lastfm_json); //refresh the cache file with new content } //cachefile is fresh enough... outputting data $parsed_json = json_decode(file_get_contents($cacheName), true); if(!empty($parsed_json)){ for($i=0; $I<$songs; $i ){ $artist[$I] = $parsed_json['recenttracks']['track'][$I]['artist']['#text']; $title[$I] = $parsed_json['recenttracks']['track'][$I]['name']; $url[$I] = $parsed_json['recenttracks']['track'][$I]['url']; $timesong[$I] = $parsed_json['recenttracks']['track'][$I]['date']['uts']; $playedon[$I] = $parsed_json['recenttracks']['track'][$I]['date']['#text']; if(!empty($parsed_json['recenttracks']['track'][$I]['@attr'])){ $now_playing[$I] = $parsed_json['recenttracks']['track'][$I]['@attr']['nowplaying'];} $tempo = time() - $timesong[$I]; //seconds passed $minutes = (int)($time / 60); $hours = (int)($minutes / 60); $days = (int)($hours / 24); if (isset($now_playing) && $now_playing[$I]=='true') { echo "<div class='lastfm-homepage'><a title='Now playing' href=".$url[$I]." target='_blank'>$artist[$I] - $title[$I]</A> now <img src=\"/img/lastfm_now.gif\" alt=\"lastfm listen icon\" /></div>"; } else if ($days >= 1) { echo "<div class='lastfm-homepage'><a title='$playedon[$I] GMT' href=".$url[$I]." target='_blank'>$artist[$I] - $title[$I]</A> " . $days . " day(S) ago</div>"; } else if ($hours >= 1) { echo "<div class='lastfm-homepage'><a title='$playedon[$I] GMT' href=".$url[$I]." target='_blank'>$artist[$I] - $title[$I]</A> " . $hours . " hours ago</div>"; } else if ($minutes > 1 && $minutes < 60) { echo "<div class='lastfm-homepage'><a title='$playedon[$I] GMT' href=".$url[$I]." target='_blank'>$artist[$I] - $title[$I]</A> " . $minutes . " minutes ago</div>"; } } } else echo "Sorry, Last.fm is down<br />"; ?>
Problems or suggestions? Please leave a comment below!