logo

Get Recent Tracks Info From Last.fm With PHP

logo

Last.fm is a Music community which has infos and sample tracks of nearly all of the artists. Last.fm members can scrobble their listened tracks into their database. So the users can generate a chart for their loved tracks, recent listened tracks, and know about similar artists/songs.
Last.fm also has a nice radio system (which is only unlimited to their subscribed users), which can be started on any kind. Like global tags (like death metal, pop, dance, classical, r&b etc.), Artists (like metallica, opeth, inna, bach, jay-z etc.) or any other methods.

Since last.fm provides a good api and ready-to-use dynamic signature pictures, I needed a code for last.fm recent tracks for my personal need. PHP Api was needlessly heavy, and the image wouldn’t suit my need, and there was no other working code online, so I’ve made a code for myself. Usage is plain simple. You just have to call the function like this:

$thelist=lastfm_recenttracks("lastfm_username",5);
foreach ($thelist as $eachsong) {
	echo "Timestamp: ".$eachsong[0]."<br>";
	echo "Artist: ".$eachsong[1]."<br>";
	echo "Title: ".$eachsong[2]."<hr>";
}

5 is the limiter. You can’t get more than 10 recent tracks with this method because of the last.fm’s limitations.

Easy, isn’t it?

Here’s the whole code with function and example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php 
//PHP Last Fm Recent Tracks Script 1.0
//Author: SoulSmasher ©2009
//web: http://www.soulsmasher.net
 
//Enter your last.fm username 
$username="your_lastfm_user";
//Amount of data to be shown (not more than 10)
$amount=5;
 
 
// Do not edit anything below unless you know what you're doing
function lastfm_recenttracks($lastfm_username,$amount=5) {
	$list=file("http://ws.audioscrobbler.com/1.0/user/$lastfm_username/recenttracks.txt"); 
	$i=0;
	$pieces=array();
	$temp=array();
	while ($i<$amount) {
		//each piece's pieces array structure: 0=>$timestamp, 1=>$artist, 2=>$title
		$pieces[$i]=explode(",",$list[$i]);
		//now $pieces[$i][0] is our timestamp of current song, $pieces[$i][1] are other parts of the song info
		$temp[$i][0]=$pieces[$i][1];
		//I saved the other part of the track info (track, artist) to $temp[$i][0] for later to use substr. Because some songs may have " - " someting like "Oh God - Please Help me!"
		$temp[$i][1]=explode("---",preg_replace('/[^0-9a-z\s\`\~\!\@\#\$\%\^\*\(\)\; \,\.\'\/\_\-]/i', '-',$pieces[$i][1])); //file() messes up with the " - ", don't know. Tried on 2 different servers, both same. it changes it to some special char which hardens the job. Instead, I changed them to - char and join them to use with explode
		$pieces[$i][1]=trim($temp[$i][1][0]);
		//now $pieces[$i][1] is artist
		$pieces[$i][2]=substr($temp[$i][0],(strlen($pieces[$i][1])+4)); //4 , because " - " this makes 3 chars, and a file() shows - as a false character, and it makes one extra character, that's why it's 3+1=4
		//$pieces[$i][2] is now title
 
		//echo "timestamp: ".$pieces[$i][0]."<br>";
		//echo "artist: ".$pieces[$i][1]."<br>";
		//echo "title: ".$pieces[$i][2]."<hr>";
		$i++;
	}
 
for($j=0;$j<$i;$j++) { unset($temp[$j][0],$temp[$j][1]); } //for memory cleanup
 
return $pieces; //either returns the array, or uncomment echo statements and change this to return true; for echoing. I prefer returning the array
}
 
$lastsongs=lastfm_recenttracks($username,$amount);
foreach ($lastsongs as $thesongs) {
	echo "Timestamp: ".$thesongs[0]."<br>";
	echo "Artist: ".$thesongs[1]."<br>";
	echo "Title: ".$thesongs[2]."<hr>";
}
?>

Enjoy ;)

VN:F [1.9.3_1094]
Rating: 6.0/10 (13 votes cast)
VN:F [1.9.3_1094]
Rating: -2 (from 6 votes)
Get Recent Tracks Info From Last.fm With PHP, 6.0 out of 10 based on 13 ratings
blog comments powered by Disqus
logo
logo
Powered by Wordpress | Designed by Elegant Themes