Using the BRI RESTful playlist API

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.


example.php

<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>

itunes.php


<?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);

 ?>


Reformatting date / time stamp to a human readable format


$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


More...

Bob Marley & The Wailers: "Stir It Up"

Aired: 2018-05-27 15:02:36
More...

Widespread Panic: "Boom Boom Boom"

Aired: 2018-05-27 14:57:11
More...

Blackberry Smoke: "Let Me Down Easy"

Aired: 2018-05-27 14:52:31
More...

Melissa Etheridge: "I WANT TO COME OVER (Edit)"

Aired: 2018-05-27 14:49:38
More...

Polyphonic Spree: "Hold Yourself Up"

Aired: 2018-05-27 14:41:34
More...

Father John Misty: "Mr. Tillman"

Aired: 2018-05-27 14:37:14
More...

National: "Guilty Party"

Aired: 2018-05-27 14:33:50
More...

Bruce Cockburn: "LOVERS IN A DANGEROUS TIME"

Aired: 2018-05-27 14:25:20
More...

Beck: "Colors"

Aired: 2018-05-27 14:21:06
More...

Meiko: "Under My Bed"

Aired: 2018-05-27 14:16:52
More...

Ray Lamontagne: "Such a Simple Thing"

Aired: 2018-05-27 14:08:55
More...

Depeche Mode: "POLICY OF TRUTH (4:01)"

Aired: 2018-05-27 14:04:04
More...

Round Mountain: "Mama Sweet Mama"

Aired: 2018-05-27 13:59:57
More...

MGMT: "Me and Michael"

Aired: 2018-05-27 13:56:36
More...

Massive Attack: "TEARDROP"

Aired: 2018-05-27 13:51:48
More...

Iron & Wine: "New Mexico's No Breeze"

Aired: 2018-05-27 13:44:09
More...

Dave Matthews Band: "Samurai Cop (Oh Joy Begin)"

Aired: 2018-05-27 13:39:36
More...

King Tuff: "Ultraviolet"

Aired: 2018-05-27 13:35:33
More...

Metric: "Sick Muse"

Aired: 2018-05-27 13:27:00
More...

Rickie Lee Jones: "The Last Chance Texaco"

Aired: 2018-05-27 13:22:46
More...

Scott Fisher: "Reckless Heart"

Aired: 2018-05-27 13:18:39
More...

Ziggy Marley & the Melody Ma: "Joy And Blues"

Aired: 2018-05-27 13:05:50
More...

Richard Thompson: "Hand Of Kindness"

Aired: 2018-05-27 13:01:39
More...

Old 97's: "Dance With Me"

Aired: 2018-05-27 12:55:45
More...

Ashley Monroe: "Orphan"

Aired: 2018-05-27 12:53:01
More...

Van Morrison: "Baby Please Don't Go"

Aired: 2018-05-27 12:48:32
More...

The Wooden Birds: "Sugar"

Aired: 2018-05-27 12:41:45
More...

Florence + The Machine: "Hunger"

Aired: 2018-05-27 12:34:58
More...

: ""

Aired: