Location: PHPKode > scripts > Last.fm Mood > Lastfm_Mood.php
<?php
/**
 * This class is used to return data from last.fm based on a specified tag, no validation on api_key or tag..
 * Queries to hide@address.com 
 *
 * @author Daniel Moffat
 */

class Lastfm_Mood {

    public $tag;
    private $api_key;

    public function __construct($tag = NULL,$api_key = NULL) {
        $this->tag = $tag;
        $this->api_key = $api_key;
    }

    public function getTag(){
        return $this->tag;
    }

    public function getArtist(){
        $lastfm_feed = 'http://ws.audioscrobbler.com/2.0/?method=tag.gettopartists&tag=' . $this->tag . '&api_key=' . $this->api_key;
        $xml = simplexml_load_file($lastfm_feed);
        $name = $xml->topartists->artist->name;
        return $name;
    }

    public function getArtistTrack($type = 'single'){
        //Feed and load XML.
        $feed = 'http://ws.audioscrobbler.com/2.0/?method=artist.gettoptracks&artist='.$this->getArtist().'&api_key='.$this->api_key;
        $xml = simplexml_load_file($feed);
        //If type is single give back a string with the top track
        if($type == 'single'){
            $trackName = $xml->toptracks->track->name;
            return $trackName;
        }
        //If type is list give back an array with all top tracks
        elseif($type == 'list'){
            $tracks = array();
            foreach($xml->toptracks->track as $track){
                $trackName = $track->name;
                $tracks[] = $trackName;
            }
            return $tracks;
        }
    }

    public function getLink($artist,$trackName){
        $formatting = '+';
        $formattedArtist = str_replace(' ',$formatting,$artist);
        $formattedTrackName = str_replace(' ',$formatting,$trackName);
        $lastfmUrl = 'http://last.fm/music/'.$formattedArtist.'/_/'.$formattedTrackName.'/';
        return $lastfmUrl;
    }

    public function getButton($link,$text=NULL){
        return "<a href=$link><img src=images/lastfm.jpg /><p>".$text."</p></a>";
    }

}
?>
Return current item: Last.fm Mood