Update for 15 June 2006: Frederik M.J. Vestre has reworked my code to be able to use the XML feeds from last.fm. Kudos to him! If you want to give it a try, check out his Audioscrobbler XML data mining page. (as noted below, this page has changed location to the currently-linked folk.ntnu.no domain — 08 August 2007)

Update for 29 October 2005: I’ve modified things to no longer use cURL, so this should work for a wider range of users. I’ve also added the ability to display any of audioscrobbler’s text-based lists, including top albums and top artists. You can also put text before and after each list entry, which will allow you to turn these lists into proper HTML lists. Comments are welcome below. :)

If you have a last.fm account, you can use this PHP code to display your most recently-played songs on any webpage. Just download the last.fm info PHP file, rename it to “lastfm_info.php” (without the quotes), and stick it in the directory on your webserver that has the file you want your songs displayed on. WordPress users, put it in the same directory as your wp-config.php file.

Once you’ve got that done, add the following line to the file that you want your songs displayed in:
<?php include('lastfm_info.php'); ?>

Then, in that same file, put the following line where you want your songs displayed:
<?php display_lastfm_info( username, number_of_items, list, prefix, suffix, stats ); ?>

The only mandatory argument is your username. The other arguments are:

  1. number_of_items – the number of items you wish to be displayed, up to a maximum of ten. If you do not define this, then five items will be displayed.
  2. list – which list of items to display. This can be “topartists” (displays your Top Artists list), “topalbums” (displays your Top Albums list), “toptracks” (displays your Top Tracks list), “tags” (displays the tags you use most often to tag songs), “friends” (displays your friends), “neighbours” (displays your neighbours), or “recenttracks” (displays the tracks you recently listened to). If you do not define this, then your recent tracks will be displayed. This argument is case-insensitive.
  3. prefix & suffix – strings to display before and after each item. This can be used to present your list as an HTML list by setting prefix to <li> and suffix to </li>. If these are not defined, then nothing will be pre- or appended.
  4. stats – whether or not to display stats along with each item, where applicable. This applies to the “topartists”, “topalbums”, “toptracks”, and “tags”, by appending the number of each item after the item, in parentheses. For the “neighbours” list, it appends the percentage of commonality that you share with that neighbour. For example, if I have listened to Underworld 183 times, then with “stats” set to 1, this function will print “Underworld (183)”. If you wish stats to be displayed, set this to 1. If you do not define anything, then no stats will be displayed.

So, for basic functionality, if you just want to display your five most-recently played songs, you would call the function as:
display_lastfm_info( ’CanSpice’ );

If you want to display your ten most-listened to artists in an HTML list, with the number of listens after each artist, you would call the function as:
display_lastfm_info( ’CanSpice’, 10, ’topartists’, ’<li>’, ’</li>’, 1 );

Notes:

  1. The directory containing this code must be writable by the user under which the webserver runs. For example, on UNIX-based computers, this is typically the nobody user. You could set the directory to be owned by the nobody group, then set group write privileges by doing chmod g+w <dir> in a shell or over FTP. If you have difficulties doing this, please contact your hosting provider.
  2. This code caches the results from last.fm in order to comply with their requirements that the webservices not be accessed more than once every second for a given user. It downloads the results to a local file, and only requests an update from last.fm if that file is more than 30 seconds old.
  3. This code uses the plaintext file made available on audioscrobbler. Future plans include using the XML file for better artist and track name differentiation, along with linking back to last.fm automatically.
  4. The single quotes surrounding the various parameters are right single quotes, which are generated by (on standard North American English keyboards) the key just to the right of the Enter key. They’re not backticks.

This software is licensed under the GNU GPL, and is Copyright © 2005 Brad Cavanagh. All Rights Reserved.