Data, Maps, Usability, and Performance

LastFM API with PHP

Last updated on March 7, 2013 in Development

Using the Last.FM API

Today I needed to quickly retrieve and store music artist photos and album covers. After looking around, I decided that Last.FM is a good place to do that and I signed up for an API key. I want to provide an artist name and run some API calls that will grab an artist photo, retrieve the top albums, and allow me to also search for a particular album. I also want to store these images on disk and convert everything to JPG format. Considering these use cases, I decided to code this up in PHP but any other server side language would also be fine. Here is my script and I am also going to include links to other implementations of pulling data from Last.FM using their API.

After a little bit of googling, I have found a good starting point for this project with this simple PHP class to retrieve artwork. I don’t really need a separate class so I changed it to a function and simplified it a bit. I also need to get top albums and any particular album so I added 2 extra functions, as well as a convert function that converts any external image into jpg format and returns it in base64 format. Here is the code:

As you can see from the code above, I did this really quickly and could improve some things like making API Key a variable, passing jpg image compression number as variable, and other things. But, it’s a busy day and this works. Here is the demo of the code above.


Using the PHP API API bindings for PHP API, Cache, and Time PHP Api v2

Tags: , ,

Facebook Twitter Hacker News Reddit More...