Sean Gofus - Web Design and Development

I love music! When I am designing or developing a new website or anything for that matter, there is nothing that inspires me more than listening to music while I create. It puts me in the right mood. What better way to show my love of music then to display what I listen to while creating things, in the footer of my website.

Where Do I Get Data?

So the first problem I had was how can I get the information on the music I listen to in real time. I am an avid fan of Spotify. Their service is great and I spend the majority of my day streaming songs from them. I could just use a Spotify PHP wrapper and make API calls to their service; however this only gets me information about music I listen to in Spotify. I also listen to music from my personal library in iTunes. How could I get this information as well? Then I remembered that I had a Last.fm account and I had enabled their Scrobbler in Spotify and downloaded it a long time ago on my computer. So everything I listen to gets Scrobbled into my Last.fm account; this is where I could get all the data I needed.

Make D'em API Calls...HOLLA!

Now that I knew where I could get my data from I needed to start writing code to make the API calls I needed to gather my data. Fortunately Last.fm has a very robust API and I found a nice PHP wrapper to interact with it called lastfm API for PHP by Matt Oakes. After I downloaded this wrapper the rest was easy.

Since I use Codeigniter as my CMS I needed to write a Model that will use this PHP wrapper and make the API call and return me the data I am looking for. This is what that Model looks like:


<?php
require $_SERVER['DOCUMENT_ROOT'].'/lastfmapi/lastfmapi.php';

class Music_model extends CI_Model {
 
function getRecentTracks()
{
   $file = fopen($_SERVER['DOCUMENT_ROOT'].'/lastfmapi/auth.txt', 'r');
   $authVars = array(
      'apiKey' => trim(fgets($file)),
      'secret' => trim(fgets($file)),
      'username' => trim(fgets($file)),
      'sessionKey' => trim(fgets($file)),
      'subscriber' => trim(fgets($file))
   );
   $config = array(
      'enabled' => false,
      'path' => $_SERVER['DOCUMENT_ROOT'].'/lastfmapi/',
      'cache_length' => 1800
   );
   $auth = new lastfmApiAuth('setsession', $authVars);
   $apiClass = new lastfmApi();
   $userClass = $apiClass->getPackage($auth, 'user', $config);
  
   $methodVars = array(
      'limit' => 5,
      'user' => 'seangofus'
   );
  
   if ( $tracks = $userClass->getRecentTracks($methodVars) ) {
      return $tracks;
    }
   else {
      die('Error '.$userClass->error['code'].' - '.$userClass->error['desc'].'');
   }
}
 
}

I know that it may look a little complicated but it's not. All it is really doing is setting up authorization so that I may make an API call (SECURITY PEOPLE), then passing the configuration settings to the PHP class which in this case is the User class and then making the API call. If everything is successful it will return the results back to my Controller in one big array of information. If there is an error it will output a generic message.

Bringing It All Together

Once I successfully get the data returned to me from Last.fm this is a sample of what the data looks like:


[0] => Array
   (
      [name] => Shell Games
      [mbid] => 
      [url] => http://www.last.fm/music/Bright+Eyes/_/Shell+Games
      [date] => 1330712740
      [streamable] => 1
      [artist] => Array
         (
            [name] => Bright Eyes
            [mbid] => 78f797e3-4913-4026-aad0-1cd858bd735b
         )
       [album] => Array
          (
             [name] => The People's Key
             [mbid] => 
          )
       [images] => Array
          (
             [small] => http://userserve-ak.last.fm/serve/34s/55927347.png
             [medium] => http://userserve-ak.last.fm/serve/64s/55927347.png
             [large] => http://userserve-ak.last.fm/serve/126/55927347.png
          )

)

This array contains everything I need to format my recently listened to tracks the way I want. I stored this information in an array name $recentTracks then passed it to my footer View where I then parsed and styled the information. As you can see below in the footer everything came out pretty nice. Not only does it show my recently listened to tracks but it will also show what the current track I am listening to if any.


COMMENTS

  1. Sean

    Hey Alexander sorry for replying so late on this. The auth file contains my API key, my secret key, and my username. In that order.

  2. Alexander

    What do you have in your auth file?