LastFM API with PHP
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 last.fm 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.
Related:
Using the PHP Last.fm API
last.fm API bindings for PHP
Last.fm API, Cache, and Time
Last.fm PHP Api v2