Using the PHP Last.fm API

If you’re like me and listen to music while coding, and you like keeping a record of the songs you’ve listened to, then you’ve probably heard of Last.fm.

Last.fm is a social website which has a music recommendation engine at its core. It keeps track of its users’ music tastes, and offers events, wikis, and discographies for artists. All of the data is gathered using a Audioscrobbler database plugin installed with each user’ music player (Spotify, Winamp, iTunes, etc.). Above all, Last.fm has an rich and impressive API which developers can use to build mobile and web mashups.

If you are building a site for a band or artist and you want to make it more social using Last.fm, it’s a must to use their API. In this way artists and fans can become connected and fans can stay up to date on concerts and new albums.

In this article I show you how to query the Last FM API to get information to build a fan website for Coldplay. We start with getting the basics about the band, then get their most popular song, the contents of their albums, and a list of the events where the band is going to play for fans to get tickets.

Getting Started

In order to get an API account, you need to have a Last.fm user account first. Login with your user account and then go to www.last.fm/api/account to get an API account. When you apply for an API account you have 4 options: Commercial Use, Commercial-Promotional Use, Bespoke Use, and Non-commercial Use. Specify your application’s name, a description, and website, and you’ll be given an API key and secret.

As with all major API services, there are a bunch of different libraries available for different programming languages, including PHP. I’ll be using the PHP Last.fm API library created by Felix Bruns and hosted on GitHub. Clone the project or download the ZIP file and extract it into your working directory.

Issuing Requests

The Last.fm API is REST-based and uses XML-formatted responses, but this detail is abstracted from us by the PHP Last.fm API library. We can interface with Last.fm through the methods.

The Last.fm artist resource returns information about an artist, events the artist is performing, and the artist’s most popular tracks. To start, let’s search for an artist using the library’s Artist::search() method.

Authenticating Your Mashup

Music is a big part of social interaction; I like to see what my friends are listening to so I can potentially discover artists I’ve never heard before and become a fan. We can get user information from the API and use it in our own mashup.

It’s a bit odd that Last.fm doesn’t use OAuth as other major social sites do. Instead they use a similar approach but having a single token instead of a access token, and access token secret as the Oauth uses. A description of the flow can be found at www.last.fm/api/webauth. But again, this is mostly an implementation detail that’s been abstracted away by our library.

The first step is to request permission to retrieve information about a user. Once the mashup is authenticated, Last.fm returns to a callback URL after which we can retrieve the desired information.

A user will click on the link and be taken to the Last.fm authentication page. After the user is authenticated, she will be redirected back to site and we’ll have the token necessary to issue requests.

Conclusion

In a world that everything tends to be more and more connected, and now that Open Graph is becoming standard, connecting fans with their favorite artists its very important, and Last FM does it very well through its API. Being open allow you to enhance your community and raise awareness of your product/idea and the best way to do that it’s by having a public API.

Daniel Gafitescu is a senior PHP developer working for Pentalog in Iasi, Romania. He loves new web development technologies, and enjoys watching movies and games from English Premiership, playing soccer with friends, and most of all spending time with his newborn son and his lovely wife.