Simple PHP Code To Get Last Played Tracks

1st December 2011 - 5 minutes read time

The other day I was approached by a friend (Julie Cheung) and asked if I could create some code that would display a list of last played tracks from Julie isn't a PHP developer and so the code I gave her had to be easily understandable so that she could edit it herself if needed. The following code is what I came up with.

  1. $username = 'philipnorton42';
  2. $scrobbler_url = "" . $username . "/recenttracks";
  4. if ($scrobbler_xml = file_get_contents($scrobbler_url)) {
  5. $scrobbler_data = simplexml_load_string($scrobbler_xml);
  7. echo '<ul>';
  8. foreach ($scrobbler_data->track as $track) {
  9. $string = '<li>';
  10. $string .= '<div class="cover"><img class="cover" height="28" src="' . $track->image[0] . '" width="28" /></div>';
  11. $string .= '<p><span class="title">' . $track->artist . '</span><br />' . $track->name . '</p>';
  12. $string .= '<p>Played: ' . $track->date . '</p>'; $string .= '</li>';
  13. echo $string;
  14. }
  15. echo '</ul>';
  16. }

What this code does is to grab the XML feed containing the latest played songs from and convert them into a set of list elements containing some information about the tracks. This is done very simply by using the simple_xml_load_string() function, which works quite well for most situations and in this case only destroys a small amount of information that isn't really needed. Each track contains information about the artist and track that was played, including album artwork. Here is a breakdown of the information available in the feed, with the latest song in my own playlist as an example.

  • artist : The name of the artist (e.g. "The Black Dahlia Murder").
  • name : This is the name of the track (e.g. "Deathmask Divine").
  • streamable : This is a boolean value (as a 1 or a 0) that states if the track can be streamed.
  • mbid : The mbid is an ID of the song, but is lost when the XML feed is passed through the simplexml_load_string() function. This doesn't matter, however, as we probably won't need it when printing out the results.
  • album : This is the name of the album that the track belongs to (e.g. "Nocturnal").
  • url : This is a URL that links back to the track information on the site. For the example I gave above this is
  • image : This is an array if different images for the album cover. Although the sizes are lost in the translation from XML the array translates to the following sizes:
    • 0 : Small image size, 34px by 34px.
    • 1 : Medium image size, 64px by 64px.
    • 2 : Large image size, 128px by 128px.
    • 3 : Extra large image size, 300px by 300px.
    You can see in the example above that I have selected the small image size in the array by using $track->image[0].
  • date : This is the date and time that the track was played on (e.g. "21 Nov 2011, 15:28").

One addition to this code would be to stop it pulling in the entire feed on every page load, which isn't really needed. The addition of a small amount of caching code is ideal to keep page load times down (especially as we are relying on a third party service). We only need to store the data in a separate file for about 3 minutes or so (the average playtime of a song) so this time check is also added to the code.

  1. <?php
  3. $username = 'philipnorton42';
  4. $scrobbler_url = "" . $username . "/recenttracks";
  6. $scrobbler_cache_file = 'scrobbler_' . $username . '_data.cache';
  8. if (file_exists($scrobbler_cache_file)) {
  9. if (time() - filemtime($scrobbler_cache_file) --> 180) {
  10. // if the file was created more than 3 minutes ago then delete.
  11. unlink($scrobbler_cache_file);
  12. } else {
  13. $scrobbler_url = realpath('./' . $scrobbler_cache_file);
  14. }
  15. }
  17. if ($scrobbler_xml = file_get_contents($scrobbler_url)) {
  18. $scrobbler_data = simplexml_load_string($scrobbler_xml);
  20. if (!file_exists($scrobbler_cache_file)) {
  21. file_put_contents($scrobbler_cache_file, $scrobbler_xml);
  22. }
  24. echo '<ul>';
  25. foreach ($scrobbler_data-&gt;track as $track) {
  26. $string = '<li>';
  27. $string .= '<div class="cover"><img class="cover" height="28" src="' . $track->image[0] . '" width="28" /></div>';
  28. $string .= '<p><span class="title">' . $track->artist . '</span><br />' . $track->name . '</p>';
  29. $string .= '<p>Played: ' . $track->date . '</p>';
  30. $string .= '</li>';
  31. echo $string;
  32. }
  33. echo '</ul>';
  34. }

You can see the code (or at least a variant) in action on Julie Cheung's site. If you want my to write something like to for your own blog then let me know, as long as I can create a blog post out of it (and perhaps a backlink) I'm happy to help.



Nice code, very easy to understand. You just taught me some cool things I have yet to learn about PHP and caching also.

Curtis (Fri, 12/02/2011 - 07:42)


Simple PHP code, though I got it mixed up with the one I'm working on now. Haha. But thanks for this! *bookmarks*

Sue Crawford (Tue, 02/07/2012 - 09:43)


Works like a charm, unlike all the WordPress plugins I've tried

Matt (Fri, 04/20/2012 - 22:45)

Great code! Is there any way to implement this to a group on Lets say display all users from the group and what there last track was? Cheers, Danyo

Danyo (Wed, 06/05/2013 - 14:38)

Thanks :) The only comparable feed available for groups is the journal feed (eg. The alternative here is to use the API to extract the data you need from the system. This requires the use of an API key, which you have to apply for. Using that you can then either get a list of the most listened tracks that week with the getWeeklyTrackChart method ( or via getting members of the group through getMembers ( and then looping through each member to find their latest tracks. Of course, the simplest way to go might be to have a list of users and loop through them to collate their track listings. You'll need to compare the date and time of each entry to put them in order, but it shouldn't be that hard :)
Thanks for the reply. I have managed to do it! With you above code is there anyway to limit the amount of tracks being pulled in from each user? Currently its 10, i would only like 1 track to be pulled in. Cheers, Dan

Danyo (Thu, 06/06/2013 - 09:29)

I found this script very useful... the simplicity of the code loads quite fast. Like a previous post, I would like to display only one track. I have implemented your solution, it works like a charm. I was curious... when I'm listening to music, will apply a <track nowplaying="true"></track> to the XML feed. When this occurs, the script displays two tracks. Is it possible to disregard the <track nowplaying="true"></track> so that the script displays only one track, regardless of playing music or not? Thanks, Scott

Scott (Wed, 04/02/2014 - 17:54)

You'll probably want something like:
  1. foreach ($scrobbler_data->track as $track) {
  2. if ($track->nowplaying == 'true') {
  3. continue;
  4. }
  5. ...
Note that this is untested, but what what you said in your comment the track should have a 'nowplaying' property, which should contain a value of some sort. This is probably a string saying 'true', but it *might* get transformed into a boolean so it's worth checking. Essentially, when the nowplaying property is true then the loop skips the track in question so you won't see it in the output.
Hi, is there a way to make it like showing 2 songs instead of 11 songs? Greets, Jerry

Jerry (Thu, 03/26/2015 - 18:32)

Or is there a way, to make it like this? 1st song (now playing) 2nd song, last played | 3th song - 4th song | 5th song | 6th song So you can echo the 5th song which has played, and so forth..? Its kinda neat for Wordpress in the footer area :) Greets, Jerry

Jerry (Thu, 03/26/2015 - 18:42)

really its likely content

Baahubali telu… (Thu, 06/25/2015 - 16:35)

thanks youre saved my live

athoul (Sat, 02/17/2018 - 02:07)

Add new comment

The content of this field is kept private and will not be shown publicly.