PHP last.fm chart creator

flatspike — jurgen.strydom on November 19, 2006 at 5:59 pm

Introduction

This PHP v5 script uses the “Top 50 artists” XML data feed from the last.fm tools page and creates a chart from it for display on a webpage. The code can be adapted to be used with any of the XML feeds if needed. A CSS file is included in the source for visual adjustments to the output.

The chart in action
The chart in action.

The data feeds from last.fm
The data feeds from last.fm

About the Project

The goal of the project was to display last.fm chart information on a website. The crew from last.fm have good looking pre-built solutions, but these are limited if you want to do anything out of the ordinary. Fortunately last.fm gives free access to this information. Data feeds are available in many forms from the last.fm website but the XML feed is the one used in this project.

Requirements

The basic structure of the code for this project will be explained. To follow this project you need a testing server with PHP5 installed and some knowledge of PHP. If you do not have hosting you can setup your own. Xampp is a free tool and it takes the hassle out of setting up a personal web server on your computer, it is available at Apache friends.

Please note you need a last.fm account, and it should have some recorded statistics already, without those this project will be a fruitless exercise.

Personal Note

This was done for fun and should be viewed in the same way. I hope this helps you to achieve your own goals. Enjoy!

Problem

Getting your music data from last.fm in an automated and reliable way and then parsing the data from the data feeds to use the information you need for your implementation. Creating a cool display from data is less critical for this project but important for the end result.

Please note the graphics falls outside the reach of this document but my full code is available for download at the bottom of this page and it includes a possible graphic interface.

Data Feeds

The first step is to obtain the data needed for the chart. This can be obtained by anyone with a last.fm account. In the main tool bar at the top of the page there will be a tools button, click on this. A page with an overview should be displayed, with other tabs next to it. We are interested in the data feeds tab, so click on it. The direct link is www.last.fm/tools/feeds/. Here you will find a list of the available feeds. This project uses the Top Artists feed, but can be easily adapted for any of the other feeds. The available data feeds are:

  • Recent Tracks: 10 recently played tracks
  • RecentWeekly Artists: your most recent weekly artist chart
  • RecentWeekly Tracks: your most recent weekly track chart
  • Top Artists: 50 most played artists from your music profile
  • Top Tracks: 50 most played tracks from your music profile

Underneath each of the feeds there are icons with acronyms in them. The one we are interested in is the XML1 data feed. It is available for each of the feeds. Right click on one of them and save it to your desktop, if you don’t have a program assigned for opening .xml files use wordpad or any other text editor. Now open the file and peek inside, you should see lost of text formatted with pointy brackets and back slashes, this is XML (an example file can bee found in the appendix of the documentattion). Next we will see how to use this data in PHP.

XML Data Parsing

Now that we have the XML file we can start to extract the data we need from it. The magic gets done with the simple XML command in php. First you set your directory with a variable like so:

$file = “http://ws.audioscrobbler.com/1.0/user/Alkine/topartists.xml”;

This is the one I used. Alkine is my profile name on last.fm so you should replace it with your own profile name. Then the magic function gets called to do the dirty work:

$xml = simplexml load file(”$file”);

This returns an array with the structure of your XML file. For a quick look at your array use:

print_r($file);

To access this array, lets say you want the name of the artist appearing first in this list, the following code can be used:

echo $xml->artist[0]->name;

Note the similarity of the call to the variable in the array, and the structure of the XML file. For the example XML file in the appendix of the documentation should return “Muse”. Please take note that the first element is number 0, the second element is number 1, the third is 2 and so on. If you have 50 artists in your file, they will be number 0 - 49 in the array.

You now have the knowledge to make all kinds of fancy websites powered by the last.fm feeds.

Summary

We wanted to create our own music charts based on the information available at last.fm. I have shown that with a few really simple commands we can extract all the information from the data feeds and build our own exiting charts. My own version of this is running at my music page but this is far from the limit in terms of what can be achieved. Any comments are welcome, write me an e-mail and tell me what you have achieved, if you found a bug, or just want to exchange links.

References

last.fm homepage
last.fm data feeds

Files

last.fm chart creator PHP source

Documentation

last.fm chart generator documentation


Add to:
del.icio.us:PHP last.fm chart creator  digg:PHP last.fm chart creator  reddit:PHP last.fm chart creator

Related:

0 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. | Flat Spike