jul
16
2010

Acceder a Last.fm con php + curl

Con este código en php podemos obtener las carátulas de tus mas recientes pistas de Last.fm, utilizando la librería curl y puro php.

<?
 
function GetLastfm($user, $limit){
 
	#first, try to get the cache file
	$lastfmCacheDir = './cache/lastfm/';
	$filename = $lastfmCacheDir . 'lastfm';
	if (file_exists($filename)) {
		$filedate = filemtime($filename);
		$FileAge = time() - $filedate;
		if($FileAge < 3600){
			#cache is < 1 hour old			
			return file_get_contents($filename);
		}
	}
 
	#get the URL to the cache directory, for serving up images
	$thispath = $_SERVER['REQUEST_URI'];
    $thispath = explode('/', $thispath);
    $lastfmCacheUrl = $thispath[count($thispath)-2] . '/cache/lastfm/';		
 
	#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);			
			if (!file_exists($lastfmCacheDir . $localfilename)) {				
				$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()
{
  if( !function_exists("curl_init") &&
      !function_exists("curl_setopt") &&
      !function_exists("curl_exec") &&
      !function_exists("curl_close") ) return false;
  else return true;
}
function cURLdownload($url, $file)
{
  if( !cURLcheckBasicFunctions() ) return false;
  $ch = curl_init();
  if($ch)
  {
    $fp = fopen($file, "w");
    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);
      fclose($fp);
      return true;
    }
    else return false;
  }
  else return false;
}
 
/*
 * Adapted from code found in php documentation comments
 */
function file_name_from_url($url) {
    if($url===null || strlen($url)<= 0) {
        return null;
    }   
    $url= explode('?', $url);
    $url= explode('/', $url[0]);
    $basename= $url[count($url)-1];
 
    return $basename;   
}
 
?>

Saludos

Leave a comment

Patrocinador

Siguenos

Suscribete

Escribe tu correo:

Recibirás un correo con todos los articulos escritos en ZoneArtCss durante el día.

Correo @zoneartcss.com

Obtén una cuenta de @zoneartcss.com gratuita