Display your Last.fm rankings using PHP 4’s XSLT support
Friday, February 1st, 2008With all the exciting recent news about Last.fm, I thought I would document a simple bit of code I added to my site the other day.
Last.fm offers a number of Flash-based widgets you can add to your website. Unfortunately, this doesn’t give you much flexibility and, of course, requires Flash. But you, dear friend, have a site written in PHP, and the rankings are just XML files. There is a better way.
Looking around on the web, there are some good instructions and recommendations for using PHP 5’s object-oriented XML support. But, as we know, not everyone is using PHP 5. Here’s what I did on my PHP install, which includes the DOM/XML and DOM/XSLT extensions.1
Write your XSL Transformation
The first step is to write your XSL Transformation, or XSLT, a special XML “program” which takes an XML file and reformats it into another XML file. Remember when you learned in Algebra class what a function was? An XSLT defines a function from XML to XML. In our case, we need to take a special proprietary XML file like the this one for my weekly top artists and return some solid XHTML.
Let’s take a look at the XSLT I used: (it helps to take a look at the original XML file at the same time.)
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="weeklyartistchart"><ol><xsl:apply-templates select="artist[position() < 6]"/></ol></xsl:template><xsl:template match="artist"><li><a><xsl:attribute name="href"><xsl:value-of select="./url"/></xsl:attribute><xsl:value-of select="./name"/></a><span><xsl:value-of select="./artist"/></span>: <xsl:value-of select="./playcount"/></li></xsl:template></xsl:stylesheet>
The full spec description will give you all the juicy details, but you really only need a few basic details (or you can just steal my code). First, we note the <xsl:template match="weeklyartistchart"> and <xsl:template match="artist"> items. Each of these code blocks describe what to do to each <weeklyartistchart> and <artist> nodes, respectively, in the input XML. In the code above, when a <weeklyartistchart> is found, an ordered list is opened and the artist template is applied to the first five (position() < 6) <artist> nodes. In the artist template (<xsl:template match="artist">), the script takes each <artist> and prints a list item with the name of the artist, with a link to the url given in the original <artist>’s <url> subnode.
Process the XML with your XSLT
Once your XSLT is written, save it to a file, like last.fm.xml. Now we’ll use the DOM/XSLT extension and apply this XSLT file to the live weekly artist chart XML file from last.fm. Here’s the code I used:
$chartxml = domxml_open_file("…/weeklyartistchart.xml");$xslt = domxml_xslt_stylesheet_file("last.fm.xsl");$charthtml = $xslt->process($chartxml);echo $charthtml->dump_mem();
The code is pretty self explanatory—just four lines: 1. open the remote XML file using domxml_open_file, 2. open the stylesheet (XSL transformation), 3. apply the stylesheet to the remote XML file, and 4. echo the output.
That’s it! You can see the results here on my music page.
-
To see if these instructions will work for you, check your phpinfo for the lines “DOM/XML enabled” and “DOM/XSLT enabled”. If the items aren’t even showing up, you’re out of luck.
There are, however, other comparable methods to process XML and XSLT in PHP 4.
↩