This lightweight class makes it simple to integrate and process music info from last fm.
Buy Online for $8This 5 tabs will show music information. The class supports Trending music, Artists search, Track search, Album search & Artist info (toptracks, bio, tags, similar artists). The fifth tab will show the setup & methods of this lightweight class.
Example Pages
You need to have the module cUrl to be enabled (you can check this in phpinfo)
Change the APIKEY const to your API KEY in the lastfm class (lastfm.class.php), note: this api key is a dummy key so not valid. Also you can adjust the default search limit.
const APIKEY = 'b542e4633dc1c8sd452007747538';
const DEFAULT_LIMIT = 20;
Include & initalize class
include_once('lastfm.class.php');
$lstfm = new Lastfm();
Fallback Image
The fallback image can be found & adjust in the folder (img/standard_cover.png)
This lightweight class has the following functions. The trending & search methods have a optional limit paramater
The trending methods are set on the region based ISO3166-1 country names standard (Belgium, USA, Spain,...). Check the wikipedia for your countryname
$lstfm->trendingTracks('Belgium'); //gets the trending tracks based on a country
$lstfm->trendingArtists('Belgium', 10); //gets the trending artists based on a country limited to 10 items (limit parameter)
$lstfm->searchTracks('Soldier'); //searchs tracks based on a search term
$lstfm->searchArtists('Marco Bai') //searchs artists based on a search term
$lstfm->searchArtists('Marco Bai', 20) // same with a limit of 20 results
$lstfm->searchAlbums('Half power'); //searchs albums based on the given term
$lstfm->searchArtistInfo('Eminem', array('toptracks', 'bio')); //gets the toptracks, bio (bio text, tags & similar artists) based on a full artist name
$lstfm->showHtmlList($array, 'type'); //returns an styled unorderd (ul) html list (supports types: artists/tracks/albums)
$lstfm->findVideo($artist, $title); //returns an youtube link based on the given artist & title
Default ajax functions (artist/track/albums) will output 1 page with 10 results (ajax.functions.php) You can adjust that file in according your need
$searchTracks = $lstfm->searchTracks($trackTerm, 1 , 10);
$lstfm->showHtmlList($searchTracks, 'tracks');
The Show HTMLList method wil return a unorderd list with a structure like the example below (searchTracks)
<ul class="lastfmlist tracks">
<li class="item">
<div class="meta">
<h1>Sing for Absolution</h1>
<h2>Muse</h2>
</div>
<img width="130px" src="http://userserve-ak.last.fm/serve/300x300/61418461.png">
</li>
<li class="item">
<div class="meta">
<h1>Single Ladies (Put a Ring on It)</h1>
<h2>Beyoncé</h2>
</div>
<img width="130px" src="http://userserve-ak.last.fm/serve/300x300/93272439.png">
</li>
<li class="item">
<div class="meta">
<h1>Sing for the Moment</h1>
<h2>Eminem</h2>
</div>
<img width="130px" src="http://userserve-ak.last.fm/serve/300x300/88163203.png">
</li>
<li class="item">
<div class="meta">
<h1>Sing</h1>
<h2>Travis</h2>
</div>
<img width="130px" src="http://userserve-ak.last.fm/serve/300x300/73368188.png">
</li>
<li class="item">
<div class="meta">
<h1>And Your Bird Can Sing</h1>
<h2>The Beatles</h2>
</div>
<img width="130px" src="http://userserve-ak.last.fm/serve/300x300/100131537.png">
</li>
<li class="item">
<div class="meta">
<h1>What Your Soul Sings</h1>
<h2>Massive Attack</h2>
</div>
<img width="130px" src="http://userserve-ak.last.fm/serve/300x300/30959875.png">
</li>
<li class="item">
<div class="meta">
<h1>Sing It Back</h1>
<h2>Moloko</h2>
</div>
<img width="130px" src="http://userserve-ak.last.fm/serve/300x300/4576293.jpg">
</li>
<li class="item">
<div class="meta">
<h1>Y'all Want A Single</h1>
<h2>Korn</h2>
</div>
<img width="130px" src="http://userserve-ak.last.fm/serve/300x300/67919282.png">
</li>
<li class="item">
<div class="meta">
<h1>Singapore</h1>
<h2>Tom Waits</h2>
</div>
<img width="130px" src="http://userserve-ak.last.fm/serve/300x300/98058591.png">
</li>
<li class="item">
<div class="meta">
<h1>Billie Jean - Single Version</h1>
<h2>Michael Jackson</h2>
</div>
<img width="130px" src="img/standard_cover.png">
</li>
</ul>
Print an styled unorderd html list form trending artists based on a region based on the ISO3166-1 country names standard (Belgium, USA, Spain,...)
$trendingArtists = $lstfm->trendingArtists('Belgium');
$lstfm->showHtmlList($trendingArtists, 'artists');
Output (check the source for the structure)
Warning: Undefined property: stdClass::$error in /customers/c/7/9/downloadmarkt.be/httpd.www/lastfm-musicengine/libraries/lastfm.class.php on line 46Print the array for trending artists
print_r($trendingArtists);
Output
Array
(
[0] => stdClass Object
(
[name] => David Bowie
[listeners] => 4567208
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[1] => stdClass Object
(
[name] => Radiohead
[listeners] => 6507535
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[2] => stdClass Object
(
[name] => Coldplay
[listeners] => 7399937
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[3] => stdClass Object
(
[name] => The Beatles
[listeners] => 5261642
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[4] => stdClass Object
(
[name] => Queen
[listeners] => 5820410
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[5] => stdClass Object
(
[name] => The Rolling Stones
[listeners] => 4971690
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[6] => stdClass Object
(
[name] => Fleetwood Mac
[listeners] => 3642162
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[7] => stdClass Object
(
[name] => Daft Punk
[listeners] => 5162329
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[8] => stdClass Object
(
[name] => Red Hot Chili Peppers
[listeners] => 5964393
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[9] => stdClass Object
(
[name] => Nirvana
[listeners] => 6018052
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[10] => stdClass Object
(
[name] => The Cure
[listeners] => 4281837
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[11] => stdClass Object
(
[name] => Pink Floyd
[listeners] => 4375408
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[12] => stdClass Object
(
[name] => Michael Jackson
[listeners] => 5248733
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[13] => stdClass Object
(
[name] => The Weeknd
[listeners] => 3889927
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[14] => stdClass Object
(
[name] => Tame Impala
[listeners] => 3011098
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[15] => stdClass Object
(
[name] => Adele
[listeners] => 4513820
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[16] => stdClass Object
(
[name] => Arctic Monkeys
[listeners] => 5593301
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[17] => stdClass Object
(
[name] => Ed Sheeran
[listeners] => 3273718
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[18] => stdClass Object
(
[name] => The xx
[listeners] => 2442149
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[19] => stdClass Object
(
[name] => Massive Attack
[listeners] => 3026680
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
)
Print an styled unorderd html list form trending tracks based on a region based on the ISO3166-1 country names standard (Belgium, USA, Spain,...)
$trendingTracks = $lstfm->trendingTracks('Belgium');
$lstfm->showHtmlList($trendingTracks, 'tracks');
Output (check the source for the structure)
Warning: Undefined property: stdClass::$error in /customers/c/7/9/downloadmarkt.be/httpd.www/lastfm-musicengine/libraries/lastfm.class.php on line 81 Warning: Undefined property: stdClass::$toptracks in /customers/c/7/9/downloadmarkt.be/httpd.www/lastfm-musicengine/libraries/lastfm.class.php on line 86 Warning: Attempt to read property "track" on null in /customers/c/7/9/downloadmarkt.be/httpd.www/lastfm-musicengine/libraries/lastfm.class.php on line 86 Warning: foreach() argument must be of type array|object, null given in /customers/c/7/9/downloadmarkt.be/httpd.www/lastfm-musicengine/libraries/lastfm.class.php on line 89 No resultsPrint the array for trending tracks
print_r($trendingTracks);
Output
Array
(
)
This input box does an ajax call to (ajax.functions.php) for autocompleting tracks with a term this works with (the JS file for the event handling of the inputbox)
Create an input text field with id trackSearch and a result div with id resultTrack
<input type="text" id="trackSearch" placeholder="Trackname...">
<div id="resultTrack"></div>
HTML Output (check the ajax.function.php & main.js for the response)
Minimum 3 characters.
This input box does an ajax call to (ajax.functions.php) for autocompleting artists with a term this works with (the JS file for the event handling of the inputbox)
Create an input text field with id artistSearch and a result div with id resultArtist
<input type="text" id="artistSearch" placeholder="Artist name...">
<div id="resultArtist"></div>
HTML Output (check the ajax.function.php & main.js for the response)
Minimum 3 characters.
This input box does an ajax call to (ajax.functions.php) for autocompleting albums with a term this works with (the JS file for the event handling of the inputbox)
Create an input text field with id albumSearch and a result div with id resultAlbum
<input type="text" id="albumSearch" placeholder="Albumname...">
<div id="resultAlbum"></div>
Minimum 3 characters.
This page shows the info of 1 artist with the toptracks, bio, similar artists & tags of that artist in an array
$lstfm->searchArtistInfo('Eminem', array('toptracks', 'bio')); // returns toptrack & bio
$lstfm->searchArtistInfo('Eminem', array('toptracks'), 10); //artist info without bio and only 10 toptracks
$lstfm->searchArtistInfo('Eminem' array('bio')); //artist info without toptracks and only bio
$artistinfo->bio->cover_image
artistinfo->bio->name
$artistinfo->bio->summary
$lstfm->showHtmlList($artistinfo->bio->similar, 'artists');
$lstfm->showHtmlList($artistinfo->bio->tags, 'tags');
Output
Marshall Bruce Mathers III (born October 17, 1972), known professionally as Eminem (/ˌɛmɪˈnɛm/; often stylized as EMINƎM), is an American rapper. He is credited with popularizing hip-hop in the world and is regarded as one of the greatest rappers of all time. Eminem's global success and acclaimed works are widely regarded as having broken racial barriers for the acceptance of white rappers in popular music. While much of his transgressive work during the late 1990s and early 2000s made him a controversial figure Read more on Last.fm
Similar artists$lstfm->showHtmlList($artistinfo->toptracks, 'tracks');
Output
Output of array $artistinfo
stdClass Object
(
[toptracks] => Array
(
[0] => stdClass Object
(
[artist] => Eminem
[title] => Without Me
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[1] => stdClass Object
(
[artist] => Eminem
[title] => The Real Slim Shady
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[2] => stdClass Object
(
[artist] => Eminem
[title] => Lose Yourself
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[3] => stdClass Object
(
[artist] => Eminem
[title] => Stan
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[4] => stdClass Object
(
[artist] => Eminem
[title] => Mockingbird
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[5] => stdClass Object
(
[artist] => Eminem
[title] => Love The Way You Lie
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[6] => stdClass Object
(
[artist] => Eminem
[title] => Not Afraid
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[7] => stdClass Object
(
[artist] => Eminem
[title] => My Name Is
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[8] => stdClass Object
(
[artist] => Eminem
[title] => Sing for the Moment
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[9] => stdClass Object
(
[artist] => Eminem
[title] => Superman
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
)
[bio] => stdClass Object
(
[name] => Eminem
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
[summary] => Marshall Bruce Mathers III (born October 17, 1972), known professionally as Eminem (/ˌɛmɪˈnɛm/; often stylized as EMINƎM), is an American rapper. He is credited with popularizing hip-hop in the world and is regarded as one of the greatest rappers of all time. Eminem's global success and acclaimed works are widely regarded as having broken racial barriers for the acceptance of white rappers in popular music. While much of his transgressive work during the late 1990s and early 2000s made him a controversial figure Read more on Last.fm
[similar] => Array
(
[0] => stdClass Object
(
[name] => Bad Meets Evil
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[1] => stdClass Object
(
[name] => D12
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[2] => stdClass Object
(
[name] => Obie Trice
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[3] => stdClass Object
(
[name] => Stat Quo
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
[4] => stdClass Object
(
[name] => Paul Rosenberg
[cover_image] => https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png
)
)
[tags] => Array
(
[0] => stdClass Object
(
[name] => rap
[url] => https://www.last.fm/tag/rap
)
[1] => stdClass Object
(
[name] => Hip-Hop
[url] => https://www.last.fm/tag/Hip-Hop
)
[2] => stdClass Object
(
[name] => Eminem
[url] => https://www.last.fm/tag/Eminem
)
[3] => stdClass Object
(
[name] => hip hop
[url] => https://www.last.fm/tag/hip+hop
)
[4] => stdClass Object
(
[name] => pop
[url] => https://www.last.fm/tag/pop
)
)
)
)