Jump to content

Problem Displaying Recent Last.fm Plays


pealo86

Recommended Posts

I'm using the following script on my website, it used to work fine but now it just outputs 'error' from the else statement:

 

<?php
	$username = 'pealo86';
	$scrobbler_url = "http://ws.audioscrobbler.com/2.0/user/" . $username . "/recenttracks";
	 
	$scrobbler_cache_file = 'scrobbler_' . $username . '_data.cache';
	 
	if (file_exists($scrobbler_cache_file)) {
	  if (time() - filemtime($scrobbler_cache_file) > 180) {
	    // if the file was created more than 3 minutes ago then delete.
	    unlink($scrobbler_cache_file);
	  } else {
	    $scrobbler_url = realpath('./' . $scrobbler_cache_file);
	  }
	}
	 
	if ($scrobbler_xml = file_get_contents($scrobbler_url)) {
	    $scrobbler_data = simplexml_load_string($scrobbler_xml);
	 
	  if (!file_exists($scrobbler_cache_file)) {
	    file_put_contents($scrobbler_cache_file, $scrobbler_xml);
	  }
	  
	  // limit to 3 songs
	  $count = 0;
	  echo '<ul id="feed-lastfm">';
	  foreach ($scrobbler_data->track as $track) {
	      if ($count == 3) {
		      break;
		  } else {
			  $string = '<li>';
			  $string .= '<div class="cover">';
			  if ($track->image[1] != '') {
				  $string .= '<a href="' . $track->url . '"><img src="' . $track->image[1] . '" alt="' . $track->album . '" /></a>';
			  }
			  $string .= '</div>';
			  $string .= '<div class="content">';
			  $string .= '<span class="title"><a href="' . $track->url . '"><strong>' . $track->artist . '</strong>: ' . $track->name . '</a></span>';
			  if (!$track->date) {
				  $date = '<span class="highlight-00a2f5 currently-listening">Currently Listening</span>';
			  } else {
				  $date = 'Played: ' .$track->date;
			  }
			  $string .= '<span class="date">' . $date . '</span>';
			  $string .= '</div>';
			  $string .= '</li>';
			  echo $string;
			  $count ++;
		  }
	  }
	  echo '</ul>';
	} else {
	
	echo 'error';
	}
?>

I got the script from here:

- http://www.hashbangcode.com/blog/simple-php-code-get-lastfm-last-played-tracks-605.html

 

Does anyone have any idea what I'm missing?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.