Created
September 19, 2015 13:59
-
-
Save nothingatalldotnet/0e86ea85c5f5848d071c to your computer and use it in GitHub Desktop.
Simple Last.fm now playing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class NowPlaying{ | |
private $url; | |
private $noTrackPlayingMessage; | |
function __construct($user, $api_key){ | |
$this->url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&limit=1'; | |
$this->url .= '&user=' . $user . '&api_key=' . $api_key; | |
$this->noTrackPlayingMessage = 'nothing is playing right now!'; | |
} | |
public function getNowPlaying(){ | |
$xml = simplexml_load_file($this->url); | |
$track = $xml->recenttracks->track; | |
$nowplaying = $track->attributes()->nowplaying; | |
if($nowplaying){ | |
$artist = $track->artist; | |
$songname = $track->name; | |
if($track->image->attributes()->size == "medium"){ | |
$image = $track->image; | |
return $artist . " - " . $songname . "<br /><img src=".$image; | |
} else { | |
return $artist . " - " . $songname; | |
} | |
} | |
else{ | |
return $this->noTrackPlayingMessage; | |
} | |
} | |
public function setNoTrackPlayingMessage($messageIn){ | |
$this->noTrackPlayingMessage = $messageIn; | |
} | |
} | |
$now_playing = new NowPlaying('_USERNAME_', '_API-KEY_'); | |
$now_playing->setNoTrackPlayingMessage('not listening to anything right now'); | |
echo $now_playing->getNowPlaying(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment