PHPFront

Home > PHP > Show last songs listened to from a last.fm account
The PHP website created by its users!



Show last songs listened to from a last.fm account


AuthorTim Malone
DescriptionDisplays the last songs (from 1 to 10) played by a last.fm user. Sign up free to http://www.last.fm/ and install their plugin on your computer, then use this code in your site to show your visitors in real time what you are listenting to.
RatingThis script has been rated 5/5 (3 votes)

PHP Code


<?
// last.fm Recent Tracks Parser
// Created 21st January 2006 by Tim Malone
// E-mail: tim@timmyjnr.id.au
// Web: www.timmyjnr.id.au

// Enter your last.fm username
$as_username="tdmalone";

// Enter the number of recent songs you wish to display (cannot be more than 10)
$as_display="3";

// Do you want to display the number of minutes since the song finished?
$as_showmins=true;

// No need to edit anything else unless you know what you're doing!

$handle=fopen("http://ws.audioscrobbler.com/1.0/user/$as_username/recenttracks.txt","rb");
$contents="";
while(!
feof($handle)){
    
$contents.=fread($handle,8192);
}
fclose($handle);
if(
$as_display>10){$as_display=10;}
if((
$contents!="")&&($as_display>0)){
    
$contents=explode("n",$contents);
    if(
$as_display>1){
        echo 
"The last $as_display songs I listened to were:<br />";
    }else{
        echo 
"The last song I listened to was ";
    }
    for(
$i=0;$i<$as_display;$i++){
        
$contents[$i]=explode(",",$contents[$i]);
        
$mins=number_format((time()-$contents[$i][0])/60);
        if(
$mins<0){
            
$listened="<strong>listenting now</strong>";
        }else{
            
$listened="$mins mins ago";
        }
        if(
$as_display>1){
            echo (
$i+1).". ".$contents[$i][1];
        }else{
            
$contents[$i][1]=explode(" - ",$contents[$i][1]);
            echo 
"<strong>".$contents[$i][1][1]."</strong> by ".$contents[$i][1][0];
        }
        if(
$as_showmins){
            echo 
" <small>($listened)</small><br />";
        }else{
            echo 
"<br />";
        }
    }
    echo 
"<small>Powered by <a href="http://www.last.fm/user/$as_username/" target="_blank">last.fm</a></small>";
}
?>



Rate it: Print Print

Comments


mkeefe / 23 Jan 06

Very interesting script, I like the simplicity of it and the wonderful inclusion of comments for everyone to understand.

Matt
Name
Website
Your comment