Parsing AudioScrobbler and Last.fm Feeds with PHP

AudioScrobbler (now Last.fm) is a very cool site that helps you discover new music and share what you’re listening to with others. You just download a plugin for your music player, and it reports what you’re playing to the site. The site then displays what you’ve listened to recently, as well as what you listen to most. It also gives you musical neighbors—others that share similar music tastes. You can set up connections to friends, and join groups, where you can see what the group as a whole is listening to the most.

Each user also gets an RDF feed, a simple XML list of what you’ve listened to recently. With a little bit of PHP you can take that list, parse it, and display what you’re listening to on any web site.

View the source code for an example here.

It’s really pretty simple thanks to PHP’s XML parsing abilities. Setting it up for yourself is easy: just replace my user name with yours, and change the time zone variable if necessary. The number is your offset from GMT… Pacific time would be -8, for example. Mine is set up for Eastern time. After changing those settings, you can simply include that script in another file and you’ll have a display of what you’re listening to!

If you prefer, you can further customize the display. Just modify the bottom section to your liking. If you want to you can even display multiple songs very easily. The script gives you an array, $songs, which contains an additional array for each song. Those arrays have a few different values:

$song['title'] The title of the song
$song['artist'] The artist
$song['album'] The album the song appears on
$song['link'] A link to the AudioScrobbler page for that song
$song['date'] The date and time the song was played in Unix time stamp format. You can use the date() function to format it however you like.

Here’s the source code for another example displaying several recently played songs. Note that only the last several lines were changed from the previous example.

Update: This article was originally written on March 11th, 2005. I have updated the scripts to make use of a text file to save recent data. If the text file is too old (the default is 150 seconds), it will contact AudioScrobbler for new data, otherwise it will simply display the text file. This can give a huge performance boost, and reduce the load on AudioScrobber’s servers. I highly recommend you ensure your script has permission to write this file in the location you’ve specified.

Comments

This entry has 6 comments.

alian

alian wrote on May 21, 2005:

thank you, very cool script

Buman

Buman wrote on June 16, 2005:

Great script. Thanks

didi

didi wrote on February 18, 2006:

thanks for great script, i’ll test it later..

ToKo

ToKo wrote on March 17, 2006:

great thing!
thank you a lot!

Ivo

Ivo wrote on April 15, 2006:

Wow, nice script works wonderfull.
Just 1 thing, how do I include it proparly into WordPress, any idea?

Hope to hear it, bye

JZ

JZ wrote on June 7, 2006:

Great script - thanks! I started to write my own only to find this. However, I am thinking about making a modification - basically to always show the most recent tracks. So that instead of the "I haven’t listened to anything in a while." message, it would revert to the local file version which presumably had track info, in effect not overwriting the local file unless the new data had info. Have any of you tried this?

View more

Code

VoiceOver tips for iPhone developers I’ve been working on improving VoiceOver support for Delivery Status touch 3.0. It was…

My First WWDC Above: Apple’s live App Store wall at WWDC. It showed icons of 20,000 iPhone apps,…

Displaying a password or text entry prompt on the iPhone Writing your own iPhone or iPod touch app, and wondering how to create an alert like…

Junecloud: coming soon