Below is some example code to help get you started using the API. There are two files shown below, example.php, and itunes.php. You'll see that the PHP code in example.php interacts with the BRI playlist API, which produces the song title, artist name, date / time stamp, and a URL to query the iTunes API for additional info like cover art, and a link to buy the song on iTunes. While itunes.php interacts with the iTunes API directly.
Just put the files in the same directory (you can rename example.php to anything without changing the code) and make sure that your host allows the http wrapper for "file_get_contents". If this is disabled, you'll have to use another method to call the API, such as cURL or fopen / fread. Brownrice allows http wrapper for file_get_content by default on all our servers.
<html> <head> <style> .playlist_item{ width:520px; height:50px; background:#ececec; border:1px solid #cecece; border-radius:3px; box-shadow:1px 1px 3px #cccccc; margin:5px; padding:10px; font-family: Helvetica, Arial, Sans-Serif; } h1{ font-size:18px; margin:2px; padding:2px; } h2{ font-size:16px; margin:2px; padding:2px; } span{ font-size:12px; margin:2px; padding:2px; } </style> <script src="jquery-1.10.2.js"></script> </head> <body> <?php /* Call up the API with a station name, and a date stamp. */ $xmldata=file_get_contents("http://live1.brownrice.com/api/kbac/20141224"); /* Use the simplexml_load_string function to parse the XML returned by the API. The XML has now been converted to a PHP array. */ $xml=simplexml_load_string($xmldata) or die("Error: Cannot create object"); /* Loop through the array */ foreach ($xml as $item){ /* Print out some HTML containers... */ print('<div id="pl_'.$item->id.'" class="playlist_item">'); /* Loading iTunes data for an entire 24 hour playlist gets pretty slow, but here's one way you can negate that effect. Use the jQuery load function to call up iTunes info on click. Send the itunesapiurl to itunes.php encoded in base64 for safety. */ print('<div style="float:right;"><span style="cursor:pointer;" onclick="$(this.parentNode).load(\'itunes.php?url='.base64_encode($item->itunesapiurl).'\');">More...</span></div>'); /* Just print the artist, title, and timestamp for the playlist. */ print('<h1>'.$item->artist.': "'.$item->title.'"</h1>'); print('<span>Aired: '.$item->starttime.'</span><br>'); print('</div>'); } ?> </body> </html>
<?php /* iTunes ONLY exports JSON, they don't do XML. So we'll json_decode the data returned by getting the base64 decoded itunesapiurl passed to this script. */ $itunesresults=json_decode(file_get_contents(base64_decode($_GET['url'])), true); /* Pull specific bits of data from that returned by the iTunes API. */ $results=$itunesresults['results']; $firstresult=$results[0]; $buynowlink='<a href="'.$firstresult['trackViewUrl'].'" target="iTunes">>>> Buy on iTunes <<<</a>'; $price=$firstresult['trackPrice']; $image=$firstresult['artworkUrl100']; print('<img src="'.$image.'" style="height:50px;float:left;margin-right:10px;">'); print($buynowlink.'<br>$'.$price); ?>
$date_time_stamp = strtotime($item->starttime); $human_readable=date("l, F d g:i A", $date_time_stamp); echo $human_readable;
This will return something like:
Thursday, January 01 11:57 PM
You can manipulate the first argument to the date() function to change this formatting in a number of ways.
See the links below for more information:
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.strtotime.php