We need to accomplish two tasks. First, upload your current track data to your web site. Then, use that data to display what you are listening to on your web site.
Uploading your current track data.
Now that you've set up Kung-Tunes to speak HTTP, you need something on your server that can actually do something with the data it gets. You will be using a simple PHP script that can not only process submission of a new song but also display what it has received so far, depending on whether it is called by a browser or by Kung-Tunes.
Save the following code as a file named kt.php. Upload this file to your server in the directory you specified in the KungTunes upload preferences. Also upload an empty file named kt.txt and set its permissions to be readable and writable by everyone.
<?
if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
processSong( $HTTP_POST_VARS );
else
displaySongs();
function processSong( $var_list )
{
extract( $var_list ); // get post parameters and check
if ( is_null($t) or is_null($code) )
die( "<error>Invalid usage of script</error>" );
if ( $code != 'secret' )
die( "<error>Incorrect password</error>" );
// read old song list into array
$tunes = file( 'kt.txt' );
// forget last element if there are already more than 20 songs
if ( count($tunes) >= 20 ) array_pop( $tunes );
// create a new string with the song details
$t = date("m/d, G:i").' || '.$t.' || '.$p.' || '.$a."\n";
// add new string to array
array_unshift( $tunes, $t );
// merge array into string
$t_str = join( '', $tunes );
// write to file
$tfile = fopen( 'kt.txt', "w" );
fwrite( $tfile, $t_str );
fclose( $tfile );
}
function displaySongs()
{
echo "<table>\n";
echo "<tr> <th>time</th><th>band</th>< th>song</th><th>album</th></tr>";
// read song list into array
$tunes = file( 'kt.txt' );
foreach( $tunes as $song )
{
$bits = explode( ' || ', $song );
echo '<tr>';
for ( $i = 0; $i < 4; $i++ )
echo "<td>$bits[$i]</td>";
echo "</tr>\n";
}
echo "</table>\n";
}
?>
The code has two main routines: processSong() and displaySongs(). The first is called when the script is called by Kung-Tunes and updates the data on the server. The second routine is called by the browser and outputs a formatted list of songs that have been stored on the server.
When the script is called by Kung-Tunes, it first checks if there is a title (t) and a code parameter. You use the code parameter to authorize usage of the script, so that you will not be embarrassed by other people submitting a series of Britney Spears songs to the script. If the code parameter does not match the password string set in the script, it will bail out.
If the parameters are valid, their values are simply saved to the kt.txt text file. This file stores the last 20 songs played and also prepends each song with the date on which it was added. If the script is called via the browser, it reads in this song list file and displays the contents as a basic HTML table.
Displaying track data on your site.
The hack doesn't stop here, of course. Thanks to PHP and having your song data available on your server, the possibilities are endless. You could, for example, display the most recently played song on your web page (provided its filename ends in .php), using the following code snippet:
<?
$tunes = file('kt.txt'); // read file into array
$bits = explode(" || ", $tunes[0]); // only use topmost item
$songTimePlayed = $bits[0];
$songTitle = $bits[1];
$songArtist = $bits[2];
$songAlbum = $bits[3];
echo "<h3>latest <a href=\"http://www.kung-foo.tv/kung-tunes\">iTunes</a>
song</h3>";
echo "<p>$songTimePlayed<br />$songTitle<br />\n";
if ( $songArtist != "" )
echo "<br /><a href=\"http://www.amazon.com/exec/obidos/external-search*
102-6473795-0973730?mode=music&keyword=$songArtist\">$songArtist</a>*
\n";
if ( $songAlbum != "" )
echo "<br /><a href=\"http://www.amazon.com/exec/obidos/external-search*
102-6473795-0973730?mode=music&keyword=$songAlbum\">$songAlbum</a>\n";*
echo "</p>\n";
?>
Let's test what you have so far. Launch Kung-Tunes and open the Preferences dialog. Edit the Runtime options to look like . This tells Kung-Tunes to start up automatically and to upload info any time a different song is playing. If Kung-Tunes is not running yet, press "Start timer" in the main window.
Figure 1. Timer settings in the Kung-Tunes Preferences dialog
To see what's happening under the hood, open Window → Console and Kung-Tunes will provide a detailed transcription report. This is useful for debugging the PHP script, because any error messages returned from the server will be shown in this console.
—Adriaan Tijsseling