Like many others out there, I’ve chosen to display the album covers of my most recently listened to songs in my blog’s sidebar. It was with this expressed intention in mind that I set up a Last.fm account to start tracking my listening habits.
Present Solution
Right now I’m successfully displaying this information using Dirk’s Last.fm widget which nicely piggybacks Automatic’s Sidebar Widget. Although temporary, I’ve been very happy with this setup and I encourage anyone out there using Wordpress as a blogging platform to consider this plugin combination.
That being said, this seems like the perfect opportunity to introduce myself to API offerings from Last.fm.
Inspired in large part by J Wynia’s article on this same subject; I’ll stick to his approach and more or less follow his example to create the skeletal page he introduced about a month ago.
What’s in the Data?
The data available with the Last.fm API is currently accessible in four formats: Plain text, XML, XSPF and RSS.
XML is available for every displayed data offering while the other three are more sporadically available; because of that, we’ll stick to XML in our examples.
Before working with the data, you need to take a look at it of course. To do that, simply append “?wsdl” to the end of the XML URL (e.g., http://ws.audioscrobbler.com/1.0/user/jrob00/topalbums.xml?wsdl) and your browser (I’m using Firefox) should nicely spit out something similar to this screenshot.
Feeding PHP
Let’s take a look at a simple script to get this data into PHP. First assign the URL…
<?php $user = "jrob00"; //Your Last.FM username $url = "http://ws.audioscrobbler.com/"; $url .="1.0/user/$user/topalbums.xml";
We then need to get that XML data into something PHP can work with natively.
PHP5 users can use one simple line to put the XML data into a simpleXML object.
//PHP5
$xml = simplexml_load_file("$url");
PHP4 users however are forced to use any number of far less elegant options. If you’re planning on using PHP4 for any real amount of XML parsing it might be worth your while to look for a solid backport of simpleXML. Otherwise, I would suggest trying the minixml library which is somewhat similar to simpleXML.
Let’s take a look at the same code this time using PHP4 and minixml.
//PHP4
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_URL, $url);
$xml = curl_exec($curl);
curl_close($curl);
include_once("minixml.inc.php");
$minixml = new MiniXMLDoc();
$minixml->fromString($xml);
$array = $minixml->toArray();
This accomplishes basically the same thing as the single line of PHP5 code. The major difference noteworthy to our example is that the PHP4 result is stored in an array and not an object.
Looping & Displaying the Data
In keeping true to the inspiration for this article, let’s use Wynia’s simple three column table to display our data.
print "<table align='center' width='10%' border='1'>"; $columns=3; $i=0;
When setting up the loop to access our data we’ll need to use different snippets for PHP4 & PHP5 because of the class vs object handling.
PHP5 users:
//PHP5
foreach($xml->album as $album)
{
$i++;
$artist = $album->artist;
$album_name = $album->name;
$album_url = $album->url;
$album_image = $album->image->medium;
PHP4 users:
//PHP4
foreach ($array["topalbums"]["album"] as $album)
{
$i++;
$artist = $album["artist"]["_content"];
$album_name = $album["name"];
$album_url = $album["url"];
$album_image = $album["image"]["medium"];
Fortunately at this point, we can close both loops and end our script with the same code for both versions of PHP.
if($i==0)
print "<tr>";
print "
<td align='center' valign='top'>
<img src='$album_image'><br>
<a href='$album_url'>$artist - $album_name</a>
</td>
";
if($i==$columns)
{
$i=0;
print "</tr>";
}
}
print "</table>";
Additional Resources

9 Comments
This tutorial solved me some very important problems.
I’m very glad to tell you thank you.
Great! Now I can finally access the full plethora of LastFM XML feeds, instead of limiting myself to their RSS minority..
If you are on Dreamhost you’ll find that simplexml_load_file is disabled - so you can use this instead:
http://icanhaz.com/simplexml-replacement
and it will work - huzzah!
Is there a Last.fm .wsdl file which will include all the available methods? I will be developing using C# and Microsoft Visual Studio 2005.
All are not hunters that blow the horn
Reason why is great to be a gay

You don’t have to remember everyone’s birthdays and anniversaries. It was joke
Bob: “Emily, aren’t you afraid of death?” Emily: “I just think of it as a part of life.” Bob: “Yeah. The last part.” (Bob Newhart show/Sy Rosen)
car classic insurance texas
car classic insurance texas
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru
2 Trackbacks
[...] A brief walkthrough introducing you to Last.fm’s API with PHP4 and 5 specific examples.read more | digg story Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages. [...]
[...] to have a go at fudging someone elses code… a quick google brought me to a blog post at i.ndustrio.us where the guy outlines a how-to for pretty close to what i [...]