Using Last.fm Web Service Data Feeds with PHP
Last.fm, rather kindly, provides a plethora of web service and data access formats for developers online. Via the audioscrobbler.net web site you can request data for user profiles, artists, albums, tracks, tags, groups, forums and countries in a variety of formats including, plain text, rss, xml, xpsf and iCal, where relevant.
In this article we take a quick look at using PHP to read some of the data in plain text and xml formats within your own php scripts, and reconstituting that data in yoru own web app, widget, on your web site or blog, or elsewhere. We also look at reformulating the data in JSON format for use with your AJAX-style applications.
Accessing The XML-based Last.fm Feeds in PHP
Whether you are using PHP4 or PHP5 will hugely impact the way in which you want to access the feeds, given that the available functions for the two versions are different. In essence you simply need to call the desired data feed and then process through it.
Assuming you have the DOM XML extension compiled with PHP, this is how you would do it in PHP4 for a user's topalbums.xml feed.
-
//define the last.fm username
-
$user = "vincro";
-
//define the data feed URL
-
$baseurl = "http://ws.audioscrobbler.com/";
-
$url = $baseurl."1.0/user/$user/topalbums.xml";
-
//fetch the xml data
-
if (!$xml = domxml_open_file("$url")) {
-
echo "Error while fetching the document!";
-
exit;
-
}
-
//specify the root element
-
$root = $dom->document_element();
-
//iterate through the album elements
-
foreach($root->get_elements_by_tagname("album") as $album)
-
{
-
...
-
}
With PHP5 comes SimpleXML, and for many of the small footprint feeds provided by Last.fm via AudioScrobbler you can easily use it as a means by which to refangle the data provided:
-
//define the last.fm username
-
$user = "vincro";
-
//define the data feed URL
-
$baseurl = "http://ws.audioscrobbler.com/";
-
$url = $baseurl."1.0/user/$user/topalbums.xml";
-
//fetch the xml data
-
if (!$xml = simplexml_load_file("$url")) {
-
echo "Error while fetching the document!";
-
exit;
-
}
-
//iterate through the album elements
-
foreach($xml->album as $album)
-
{
-
...
-
}
Accessing The Plain Text-based Data
To handle simple CSV-based transaction in PHP the process is super easy and can be hadled as follows:
Reconstituting the Data in JSON format
Once you have the data script-side there are tricks for turning said data into JSON for use within JS. This is a topic slightly beyond the scope of this article but others have of course written well-crafted articles on the topic, including this excellent one: "Convert XML to JSON in PHP" over at the IBM article repository. That being said, you can also use scripts such this JS-based XML to JSON converter for performing conversions in-page.
Conclusion: The Sky's The Limit
Having reviewed much of Last.fm's own build gallery, it's clear that much of what is going on is just unecessarily replicated code and ideas. With all this user profile data and artist, album, track and tag meta data there are so many possibilities of what can and can't be done currently. The door is still really wide open to do some cool things. For one, a publicly available company scrobble aggregator so that one can track your team's recent scrobbles, soemthing that can't be done by simply creating a group on the Last.fm site.
















