linein designs

21 Mar, 2007

Display Last.fm’s Recent Tracks on Web Site with PHP Function

Posted by: john In: music| php| programing

Being a very big music buff, I love being able to see what my music habits are. I’m currently using the most up-to-date version of Winamp, which does a good job of keep a history of what I listen to, but I wanted this info on the web for the world to see.

Fortunatly, Last.fm has this sweet software that “scrobbles” or sends your track data via a third part plugin to Winamp to their servers which out puts in return to a standard text file. (check out Last.fm anyway if you’re into music – it’s a cool way to meet people with similar music tastes.)

Just call up this function and echo it as followed.

// Program: recenttracks.php
// By: linein.org
// Date: 11.3.2006
// File format: timestamp,artist - track

// your last.fm username

function recenttracks($user)
{
// location on server where the cached file will be located (it must be absolute and the folder must have full (777) permission)
$cache = "$_SERVER[DOCUMENT_ROOT]/media/recenttracks.txt";

// sets played date using PHP date
$date_format = 'g:i a m.d.Y'; // 10:31 am 11.03.2006

// display styles
$track_num_style = 'font-family: arial; font-size: 10px; color: #666;';
$artist_style = 'font-family: arial; font-size: 11px; color: #fff;';
$track_style = 'font-family: arial; font-size: 11px; color: #fff;';
$date_style = 'font-family: arial; font-size:9px; color: #666;';
$dash_style = 'font-family: arial; font-size:9px; color: #999;';

// you could add here the ability to only cache ever 30 min or so, but I like it live
filemtime($cache);
$update = @file_get_contents("http://ws.audioscrobbler.com/1.0/user/$user/recenttracks.txt");
$f = fopen($cache, "w");
fwrite($f, $update);
fclose($f);

$cache = file_get_contents($cache);
$cache = str_replace( '–', '-', $cache ); // replaces en dash with regular dash (thanks MrSomeone - http://www.last.fm/user/MrSomeone/)
$cache = explode("\n", $cache);

$track_num = 1; // starting track number (you could change to count($cache) and count down)
foreach($cache as $data)
{
if(!empty($data))
{
$info = explode(",", $data, 2); // sperates date by only seperating at fist instance of a comma (since some artist/track have comma in their names

$played_time = $info[0];
$info_track = explode(" - ", $info[1]); // seperates artist and title

$artist = $info_track[0];
$title = $info_track[1];

$listingto .= ''.$track_num.')
'.$artist.'
-
'.$title.'
('.date("$date_format", $played_time).')

';
$track_num++; // adds 1 to track number
}
}
return $listingto;
}

(download as text)

Then once you’ve called the function:

$last_fm_username = 'linein_org';
$recent_tracks = recenttracks($last_fm_username);

Example with formating.

Feel free to change the function (especially the styles) to fit your needs, but please do give credit where it’s needed. I’m currently using a modified version to not only display these tracks, but to store them into a MySQL database which allows me to keep track of my music history. check it out here

Similar Posts:

No related posts.

11 Comments »

Comment by ben

hi

thanks for the code, its great. I am using it on my site…

the only problem i am having is that the times seem to be inaccurate but that’s probably my settings!

thanks
ben

Comment by john

Send me the link and the code you’re using and I’ll take a look at it.

 
 
Comment by Jay Robinson

So if I take this text file, save as a .php to my server, and only change it so that

function recenttracks($user)
{
//$user = ‘jayrobinson83’;

// location on server where the cached file will be located (it must be absolute and the folder must have full (777) permission)
$cache = “/media/recenttracks.txt”;

this should work? Cause it is not for me. I have PHP enabled on my server, but I am new to PHP. I tried putting it in an HTML wrapper, but haven’t figured it out yet. Thanks, Jay

 
Comment by Jay Robinson

I tried de-commenting the $user thing too.

Comment by john

Send me the link to your site where you’re trying this and I’ll take a look at it. Also, what is your Last.fm user name?

 
 
Comment by Jay Robinson

My Last.fm user name is Jayrobinson83. You can see the intended use at InternetJay.com.

I am testing out this functionality on my PHP5-enabled server at internetjay.com/php/test.html and test2.html and test3.html.

I haven’t learned PHP yet but I’m anxious to use this feature. Please forgive my haste. Thanks for any help you can give.

Comment by john

Try changing your file extension to .php so the first test file would be test.php. If that does not work, save the file as test.txt and let me take a look at the code.

 
 
Comment by Jay Robinson
Comment by john

I think I see the problem. The code above in test.txt is a function that you have to call. You declared it above and now you have to use it.

To use it just add this line to the bottom of the code (just above the ?>):

echo recenttracks(‘jayrobinson83′);

 
 
Comment by Fred Yates

Hey john, I’m a front end developer and really only focus on design.

PHP is like greek to me but I’d really like to use this file on my new site. Trouble is I don’t really even know where to start.

If at all possible is there a dumbed down way to explain how to do this?

Comment by john

If you’re not interested in saving the songs you’re listening to in a database, last.fm provides two easy way to display this.

1) they have a ton of widgets that you can embed – http://www.last.fm/widgets

2) use the RSS feed for the information you need and parse it with PHP (or whatever language you choose) [feeds: http://www.last.fm/api/feeds

Just shoot me a message in the “contact” form if you have any more questions.

 
 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post



Please Support Our Sponsors

A Payday advance can be a great way to get back on track.

Banner

About

My name is John Veldboom and have been in design and web programing for just over 8 years now. Started off as a hobby in high school but it has developed into a full time job now. I always tell people that if I wasn't doing this at work each day, I would be at home doing it for free. I love it!

Please visit the contact page to drop me a message.