PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of Dan Moffat   Last.fm Mood   test.php   Download  
File: test.php
Role: Example script
Content type: text/plain
Description: Show example usage of all methods
Class: Last.fm Mood
Search for artist or tracks in Last.fm
Author: By
Last change: Added example usage of how you would get the 2nd, 3rd, 4th and so on tracks from the getArtistTrack() method.
Date: 13 years ago
Size: 1,831 bytes
 

Contents

Class file image Download
<?php

//Path to class
require_once('includes/Lastfm_Mood.php');

//Define tag and API key here
$tag = 'pop punk';
$key = '';

//Instantiate Lastfm_Mood object, it requires 2 params, tag e.g. Indie, Sad etc. and a Last.fm API key.
$lfm = new Lastfm_Mood($tag,$key);

//Prints the tag set
echo "<p>Tag name: ".$lfm->getTag()."</p>";

//Prints the top artist for specified tag
echo "<p>Artist name: ".$lfm->getArtist()."</p>";

//Prints the top track for the artist generated from getArtist()
echo "<p>Artists top track: ".$lfm->getArtistTrack('single')."</p>";

//Returns an array of top tracks from the artist generated from getArtist()
echo "<p>Artists top tracks: ".$lfm->getArtistTrack('list')."</p>";

//Returns the n'th track from the top tracks
$track = array();
$trackno = 3;
foreach(
$lfm->getArtistTrack('list') as $t){
   
$track[] = $t;
}
//Will return the 3rd result of the search
echo $track[$trackno];

//Prints a Last.fm formatted URL based on artist and trackname supplied, no validation of if they exist, use with care
echo "<p>Last.fm formatted link: ".$lfm->getLink('Millencolin', 'No Cigar')."</p>";

//Example of the above using the artist and artist track generated
echo "<p>Last.fm formatted link with dynamic content: ".$lfm->getLink($lfm->getArtist(), $lfm->getArtistTrack())."</p>";

//Generates a button, first param is link, second is text displayed
echo "<p>Last.fm 'button': ".$lfm->getButton('http://google.com','Test Text')."</p>";

//Example of above using generated link from the generated artist/trackname, uncomment line.
//WARNING: Slow! Sometimes causes timeout, use with caution.
//echo "<p>Last.fm dynamic 'button': ".$lfm->getButton($lfm->getLink($lfm->getArtist(), $lfm->getArtistTrack('single')), 'Dynamically generated goodness!')."</p>";

?>