Rasher's Toolbox

Back

lastfm_recent

mixed lastfm_recent(string user) -- Retrieve a user's recent tracks from last.fm

Description

No description available. See source for details.

Example

<?php
  $tracks 
lastfm_recent('USERNAME');
   echo 
$tracks['artist'].' - '.$tracks['name'];
?>

This will output:

matchbox twenty - Disease

Source

Published under the terms of the BSD License

<?php
function lastfm_recent($user) {
    
$url sprintf('http://ws.audioscrobbler.com/1.0/user/%s/recenttracks.xml'urlencode($user));
    
$xml = new XMLReader();
    if (@
$xml->open($url)) {
        
$ret = array();
        while (
$xml->read()) {
            switch(
$xml->nodeType) {
                case 
XMLReader::ELEMENT:
                    
/* Start elements */
                    
$element $xml->name;
                    switch(
$xml->name) {
                        case 
'track':
                            
$track = array();
                            break;
                        case 
'date':
                            
$track['date_uts'] = $xml->getAttribute('uts');
                            break;
                        case 
'artist':
                            
$track['artist_mbid'] = $xml->getAttribute('mbid');
                            break;
                    }
                    break;
                case 
XMLReader::END_ELEMENT:
                    
/* End elements */
                    
switch($xml->name) {
                        case 
'track':
                            
$ret[] = $track;
                            break;
                    }
                    break;
                case 
XMLReader::TEXT:
                    
/* Text nodes */
                    
$track[$element] = $xml->value;
                    break;
            }
        }
        
$xml->close();
    }
    else {
        
$ret false;
    }
    return 
$ret;
}
?>

Last updated: Sat Jun 14 20:02:45 CEST 2008

Valid XHTML 1.0! Valid HTML 3.2!