I'm writing a Last.FM script that shows what song you're listening to, and I'd like to be able to link the artist to the song title and everything.
How would I go about doing this? I mean, I'm assuming that I'll use str_replace()
, but how?
The way Last.FM formats their urls is like this;
http://last.fm/artist/This+Is+A+Band
So basically what I'm asking is, how would I take my $artist variable, and convert those spaces to pluses, but only for the part of the script that prints the song information.
Here's my code:
$uname = "USERNAME";
$key = "API KEY";
$json = file_get_contents("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks=" . $uname . "&api_key=" . $key . "&format=json"); // Gets your data
if (strpos($json, 'nowplaying') !== false) {
list($currentsonginfo, $crap) = explode('{"nowplaying":"true"}}', $json);
list($crap, $v2) = explode('{"recenttracks":{"track":[{"artist":{"#text":"', $currentsonginfo);
list($artist, $restinfo) = explode('","', $v2);
list($crap, $currenttrack) = explode('"name":"', $restinfo); }
$playing = $artist . ' - ' . $currentrack; // Checks if a song is playing
if ($playing == ' - ') {
echo '<a href="http://last.fm/user/' . $uname . '/">';
echo "I'm not listening to anything."; // Only if you're not scrobbling
echo "</a>";
} else {
echo '<marquee>';
echo '<a href="http://last.fm/user/' . $uname . '/">';
echo "I'm currently listening to " . $currenttrack . " by " . $artist; // It shows when you're playing music!
echo "</a>";
echo '</marquee>';
}