Create Your Own Sound Stream With PHP

If you have looked at my site recently you will see I have added a new feature into the footer. Its something I have nick named “Soundstream“. Basically its a progression on the idea of a twitter feed. But instead of using twitter, it uses XML feeds from Last.fm to create a list of the last 4 tracks I listened to. In this tutorial I will show you how I have created it, and just how easy it is to create one for yourself.

What You Need

In Order to build this you will need a server which runs PHP5, and a last.fm account. If you have not got one you can create an account for free. I personally recommend you do, as its a brilliant way to listen to your favorite music for free.

The RSS Feed

In order for our code to work, first of all we need to get a feed. There are a couple of ways we can do this. The first is using JSON to make a request to the Last.fm API to collect and display the information. However this is a slow way to do business, and and some API’s such as twitter will only allow you to make a certain number of requests per hour. Which is no good if you site is visited by a lot of people.

The second option is to get an XML RSS feed of all your latest tracks, this can then be parsed and used to display all your track info. This is my preferred option as it is much quicker and puts less load in the page, plus all the parsing can be done server side, which will cause browser bugs to be none existent.

In order to get The RSS feed you must talk to last.fm. You are permitted to pass certain parameters after the URL depending on the information you wish to get back. Here is an example.

http://ws.audioscrobbler.com/1.0/user/limitless86/recenttracks.xml?limit=2

here you can see the URL points to audioscrobbler, then passes a couple of directories, then you get to my username ‘limitless86′ then at the end I specify a limit of 2 tracks that I want to have returned. Hey presto the XML I get back looks something like this.

<pre id="line1"><span class="pi"><?xml version="1.0" encoding="UTF-8"?></span>
<<span class="start-tag">recenttracks</span><span class="attribute-name"> user</span>=<span class="attribute-value">"limitless86"</span>>
<<span class="start-tag">track</span><span class="attribute-name"> streamable</span>=<span class="attribute-value">"true"</span>>
<<span class="start-tag">artist</span><span class="attribute-name"> mbid</span>=<span class="attribute-value">"c33c2065-b1c3-4406-b066-d33a9e2ea71a"</span>>OneRepublic</<span class="end-tag">artist</span>>
<<span class="start-tag">name</span>>Someone To Save You</<span class="end-tag">name</span>>
<<span class="start-tag">mbid</span>></<span class="end-tag">mbid</span>>
<<span class="start-tag">album</span><span class="attribute-name"> mbid</span>=<span class="attribute-value">""</span>>Dreaming Out Loud</<span class="end-tag">album</span>>
<<span class="start-tag">url</span>>http://www.last.fm/music/OneRepublic/_/Someone+To+Save+You</<span class="end-tag">url</span>></pre>
<pre id="line9">                <<span class="start-tag">date</span><span class="attribute-name"> uts</span>=<span class="attribute-value">"1231262615"</span>>6 Jan 2009, 17:23</<span class="end-tag">date</span>>
</<span class="end-tag">track</span>>
<<span class="start-tag">track</span><span class="attribute-name"> streamable</span>=<span class="attribute-value">"true"</span>>
<<span class="start-tag">artist</span><span class="attribute-name"> mbid</span>=<span class="attribute-value">"516cef4d-0718-4007-9939-f9b38af3f784"</span>>Fall Out Boy</<span class="end-tag">artist</span>>
<<span class="start-tag">name</span>>Thnks Fr Th Mmrs</<span class="end-tag">name</span>>
<<span class="start-tag">mbid</span>></<span class="end-tag">mbid</span>>
<<span class="start-tag">album</span><span class="attribute-name"> mbid</span>=<span class="attribute-value">""</span>>Infinity On High</<span class="end-tag">album</span>>
<<span class="start-tag">url</span>>http://www.last.fm/music/Fall+Out+Boy/_/Thnks+Fr+Th+Mmrs</<span class="end-tag">url</span>></pre>
<pre id="line17">                <<span class="start-tag">date</span><span class="attribute-name"> uts</span>=<span class="attribute-value">"1231262409"</span>>6 Jan 2009, 17:20</<span class="end-tag">date</span>>
</<span class="end-tag">track</span>>
</<span class="end-tag">recenttracks</span>></pre>

For a full view of the different types of request you want to do check out the API.

The Code

So now you get the principle lets create a new page, name it soundStream.php. Inside this file we want to write the following code;

<html>
<head></head>
<body>
<h2>Soundstream</h2>
<?php
//get XML feed and assign
$LastFeed = new SimpleXMLElement("http://ws.audioscrobbler.com/1.0/user/limitless86/recenttracks.xml?limit=10", NULL, true);
//loop through XML and display track name and artists
foreach ($LastFeed->track as $LastTrackInfo) {
echo '<div class="SoundStream"><span class="artistName">' . $LastTrackInfo->artist[0] . '</span><span class="TrackName"><i>'. $LastTrackInfo->name[0] . '</i></span></div>';
}
?>
</body>
</html>

First we break into PHP and assign a new variable called $lastFeed and use PHP5′s SimpleXMLElement() to get the XML feed out. You will of course need to change the limit and your username to suit your own needs.

Once we have the XML feed assigned we want to loop through the returned results. What better way to do this than to use a foreach loop, which simply iterates over the returned XML array and puts the artists name and into the artistName span and the track name into the TrackName span. This is all wrapped in an echo statement which will put the spans onto the page, allowing you to style the output as you see fit.

That’s All Folks

That really is all their is to it. If you want to see the code in action check out the footer of this page, or better still look at this demo page. If your feeling adventurous why not try amalgamating your twitter and last.fm feeds into 1, to create a lifestream.

  • Digg
  • StumbleUpon
  • del.icio.us
  • Twitter
  • Google Bookmarks
  • email
  • Facebook
  • RSS

Talk to me