/ Published in: PHP
This code uses the Last.fm API to get recent tracks for a user and displays the album covers as an UL. It makes a local copy of the generated html and the album cover images and reuses those for one hour.
To use the code, you must have a Last.fm API key. Get one here: http://www.last.fm/api
This is actually my first PHP code (I'm a C# and Ruby dev), so please give me your feedback/criticism.
Expand |
Embed | Plain Text
/* * Return an UL of album covers for a user's recent tracks from Last.fm * TODO: find some way to periodically clean up the local album cover cache (cron job?) */ function GetLastfm($user, $limit){ #first, try to get the cache file $lastfmCacheDir = './cache/lastfm/'; $filename = $lastfmCacheDir . 'lastfm'; if($FileAge < 3600){ #cache is < 1 hour old } } #get the URL to the cache directory, for serving up images $thispath = $_SERVER['REQUEST_URI']; #get 20 items, then show the first $limit that have album images (not all will have images) $recentTracksUrl = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=' . $user . '&limit=20&api_key=[your api key]'; $count = 0; #load the dom with last.fm response $dom = new DOMDocument(); $dom->load($recentTracksUrl); #get all tracks and loop $xPath = new DOMXPath($dom); $tracks = $dom->getElementsByTagName("track"); $result = '<ul class=\'lastfm\'>'; foreach($tracks as $track){ $songname = $track->getElementsByTagName('name')->item(0)->nodeValue; $artist = $track->getElementsByTagName('artist')->item(0)->nodeValue; $url = $track->getElementsByTagName('url')->item(0)->nodeValue; $imgurl = $track->getElementsByTagName('image')->item(1)->nodeValue; if($imgurl != ''){ #make a local copy of the image for serving from the cache $gotlocalcopy = false; $localfilename = file_name_from_url($imgurl); $gotlocalcopy = cURLdownload($imgurl, $lastfmCacheDir . $localfilename); } else { $gotlocalcopy = true; } #make sure we got it if ($gotlocalcopy) { $result .= "<li><a href='$url' target='_blank'><img src='$lastfmCacheUrl$localfilename' alt='$artist - $songname' title='$artist - $songname'/></a></li>"; $count = $count + 1; } } if($count == $limit){ break; } } $result .= '</ul>'; #write out cache file for use next time file_put_contents($filename, $result); return $result; } /* * Adapted from code found in php documentation comments */ function cURLcheckBasicFunctions() { else return true; } function cURLdownload($url, $file) { if( !cURLcheckBasicFunctions() ) return false; $ch = curl_init(); if($ch) { if($fp) { if( !curl_setopt($ch, CURLOPT_URL, $url) ) return false; if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return false; if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return false; if( !curl_exec($ch) ) return false; curl_close($ch); return true; } else return false; } else return false; } /* * Adapted from code found in php documentation comments */ function file_name_from_url($url) { return null; } return $basename; }
Comments
Subscribe to comments
You need to login to post a comment.

this script is very nice but i want to create dynamic images. Please can you help me?