January 7th, 2007

phpNapster: An Unofficial Napster Search API

API, PHP, by jay.

phpNapster is an Unofficial Napster Search API written in PHP5. The origin comes from here and I have merely wrapped it up in a neat package.

This class allows you to search Napster via Artist, Album or Track but be fore warned that if the results come back empty Napster displays an HTTP 404 type of error page and the SimpleXML Object tends to barf on it self.

Usage

First include the phpNapster class and instantiate an instance of the class:

PHP:
  1. include_once('phpNapster.php');
  2. $pn = new phpNapster();

Next pass the search_artist function an keyword to search for and assign the results to a variable:

PHP:
  1. $keyword = 'ozzy';     
  2. $searchArtists = $pn->search_artist(urlencode($keyword));

The results will come back from the function as an associative array meaning you can access the values using a key word like artistName or artistID like this:

PHP:
  1. if($searchArtists != null){
  2.     $html_results_artist .= "<strong>".count($searchArtists)." Artists</strong>";
  3.     $html_results_artist .="<br>";
  4.     foreach($searchArtists as $artist){
  5.         $html_results_artist .= "<a href='artist.php?artistid=".$artist['artistID']."'>".$artist['artistName']."</a><br>";         
  6.     }   
  7. echo $html_results_artist         
  8. }

In the example above we are checking to see if we have any results in the array and then looping over the results in a foreach loop and assigning the values to a variable that will display the results as html.

This next example displays any albums that match the key word:

PHP:
  1. $searchAlbums = $pn->search_albums($keyword);   
  2.    
  3.     if($searchAlbums != null){
  4.         $html_results_albums .= "<strong>".count($searchAlbums)." Albums</strong>";
  5.         $html_results_albums .="<br>";
  6.         foreach($searchAlbums as $result){
  7.             $html_results_albums .= "<a href='album.php?albumid=".$result['albumID']."'>".$result['albumName']."</a><br>";       
  8.         }         
  9.     }

This last example displays a list of artists (just like the first example) but also displays each album by the artist as well:

PHP:
  1. if($searchArtists != null){
  2.         $html_results_artist .= "<strong>".count($searchArtists)." Artists</strong>";
  3.         $html_results_artist .="<br>";
  4.         foreach($searchArtists as $artist){
  5.             $html_results_artist .= "<a href='artist.php?artistid=".$artist['artistID']."'>".$artist['artistName']."</a><br>";
  6.             $albums = $pn->get_albums_by_artistID($artist->artistID);   
  7.             if($albums != null){
  8.                 $html_results_artist .=  "<ul>";
  9.                 foreach($albums as $album){  
  10.                     $html_results_artist .="<li><a href='admin_album.php?albumid=".$album->albumID."'>".$album->albumName."</a></li>";
  11.                        
  12.                 }
  13.                 $html_results_artist .=  "</ul>";
  14.             }   
  15.            
  16.                
  17.         }   
  18. echo $html_results_artist;     
  19.     }

Sorry, No Demo just yet.

Download Source

Back Top

Using Script.aculo.us to Toggle a DIV II FYI: Wordpress 2.1 Has Been Released

Responses to “phpNapster: An Unofficial Napster Search API”

  1. Thankyou very much for posting this. It solved one of my biggest problems :D

  2. Hey,

    I'm using this and its working pretty well, but I'm trying to build a 'jukebox' interface so that the office can choose songs and they get added to the playlist.

    It'd all be working except one small thing, the track IDs from the XML/Api seem different to the ones napster uses in its actual application. Does anyone know why?

  3. @Cohen ~ Sorry dude but I wrote this 2+ years ago and haven't touched it since then. I wonder if Napster has updated their API since this was first written?