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

Black Keys: "Gold On The Ceiling"

Aired: 2017-11-22 07:17:06
More...

Oil Boom: "Earful"

Aired: 2017-11-22 07:12:36
More...

Jen Cloher: "Forgot Myself"

Aired: 2017-11-22 07:04:43
More...

Coldplay: "In My Place"

Aired: 2017-11-22 07:00:58
More...

Fantastic Negrito: "Push Back"

Aired: 2017-11-22 06:57:24
More...

David Bowie: "Life on Mars"

Aired: 2017-11-22 06:53:27
More...

Kings of Leon: "Around the World"

Aired: 2017-11-22 06:49:55
More...

Tim Easton: "Next To You"

Aired: 2017-11-22 06:42:19
More...

Van William: "Revolution (feat. First Aid"

Aired: 2017-11-22 06:38:28
More...

Nino Galissa: "Krebo Cheo"

Aired: 2017-11-22 06:33:45
More...

Tyminski (Dan, AKrauss&Union: "Breathing Fire"

Aired: 2017-11-22 06:27:04
More...

Alabama Shakes: "Hold On"

Aired: 2017-11-22 06:23:14
More...

Kenny Wayne Shepherd: "Nothing but the Night"

Aired: 2017-11-22 06:18:25
More...

Eurythmics: "For The Love of Big Brother"

Aired: 2017-11-22 06:10:17
More...

David Byrne: "Monkey Man"

Aired: 2017-11-22 06:06:12
More...

Skatalites: "Rock Fort Rock"

Aired: 2017-11-22 05:59:09
More...

Elvis Costello: "Girls Talk"

Aired: 2017-11-22 05:57:13
More...

Weezer: "Happy Hour"

Aired: 2017-11-22 05:54:07
More...

Calico The Band: "Break Your Heart"

Aired: 2017-11-22 05:50:24
More...

MOON TAXI: "TWO HIGH"

Aired: 2017-11-22 05:44:19
More...

Chris Isaak: "DON'T MAKE ME DREAM ABOUT Y"

Aired: 2017-11-22 05:40:47
More...

Neil Young: "The Old Homestead"

Aired: 2017-11-22 05:32:53
More...

R.E.M.: "Pilgrimage"

Aired: 2017-11-22 05:28:45
More...

Django Django: "Tic Tac Toe"

Aired: 2017-11-22 05:21:54
More...

Interpol: "No "I" in Threesome"

Aired: 2017-11-22 05:17:58
More...

Deepak Ram: "Night in Lenasia"

Aired: 2017-11-22 05:14:23
More...

John Grant: "Pale Green Ghosts"

Aired: 2017-11-22 05:07:33
More...

Kirsty MacColl: "Us Amazonians"

Aired: 2017-11-22 05:03:25
More...

Delbert McClinton: "Giving It Up For Your Love"

Aired: 2017-11-22 04:59:44
More...

War On Drugs: "Pain"

Aired: 2017-11-22 04:55:33
More...

Rickie Lee Jones: "Satellites"

Aired: 2017-11-22 04:50:47
More...

Yo La Tengo: "Song for Mahila"

Aired: 2017-11-22 04:47:38
More...

Eilen Jewell: "You Know My Love"

Aired: 2017-11-22 04:43:11
More...

Lyle Lovett: "Funny How Time Slips Away"

Aired: 2017-11-22 04:38:44
More...

Jayhawks: "Pray For Me"

Aired: 2017-11-22 04:35:00
More...

Freddy Jones Band: "IN A DAYDREAM (Intro Edit)"

Aired: 2017-11-22 04:29:42
More...

Spoon: "Do I Have To Talk You Into"

Aired: 2017-11-22 04:25:26
More...

Rolling Stones: "Monkey Man"

Aired: 2017-11-22 04:21:10
More...

Raveonettes: "Last Dance"

Aired: 2017-11-22 04:17:51
More...

Hothouse Flowers: "THING OF BEAUTY"

Aired: 2017-11-22 04:12:14
More...

Fujiya & Miyagi: "Cat Got Your Tongue"

Aired: 2017-11-22 04:08:22
More...

Codetalkers w/Col. Bruce Ham: "Grandma"

Aired: 2017-11-22 04:03:49
More...

Arcs: "Outta my Mind"

Aired: 2017-11-22 04:00:21
More...

Ryan Adams: "To Be Without You"

Aired: 2017-11-22 03:57:01
More...

Brian Setzer Orchestra: "Lady Luck"

Aired: 2017-11-22 03:54:08
More...

Thomas Dolby: "Hyperactive"

Aired: 2017-11-22 03:50:29
More...

Emmylou Harris: "Deeper Well"

Aired: 2017-11-22 03:46:14
More...

Alex Lahey: "Lotto in Reverse"

Aired: 2017-11-22 03:42:16
More...

David Francey: "Wanna Be Loved"

Aired: 2017-11-22 03:39:54
More...

Alice Merton: "No Roots"

Aired: 2017-11-22 03:36:18
More...

Tracy Chapman: "Telling Stories"

Aired: 2017-11-22 03:32:25
More...

Fontaine Brown: "Ain't No Brakeman"

Aired: 2017-11-22 03:28:08
More...

U2: "PRIDE (In The Name Of Love)"

Aired: 2017-11-22 03:24:27
More...

Ted Leo: "Can't Go Back"

Aired: 2017-11-22 03:21:07
More...

Jill Sobule: "I Kissed A Girl"

Aired: 2017-11-22 03:17:48
More...

Dead Can Dance: "Agape"

Aired: 2017-11-22 03:10:59
More...

Widespread Panic: "Up All Night"

Aired: 2017-11-22 03:07:08
More...

Modest Mouse: "Dashboard"

Aired: 2017-11-22 03:03:03
More...

National: "Day I Die"

Aired: 2017-11-22 02:58:45
More...

Replacements: "Can't Hardly Wait"

Aired: 2017-11-22 02:55:45
More...

Turnover: "What Got in the Way"

Aired: 2017-11-22 02:52:01
More...

Squeeze: "BLACK COFFEE IN BED (4:04)"

Aired: 2017-11-22 02:47:57
More...

Blondie: "Words in My Mouth"

Aired: 2017-11-22 02:43:48
More...

Gizmodrome (Stewert Copela: "I Know Too Much"

Aired: 2017-11-22 02:39:41
More...

World Party: "PRIVATE REVOLUTION"

Aired: 2017-11-22 02:35:38
More...

Phish: "Ocelot"

Aired: 2017-11-22 02:31:58
More...

James Gang: "Alexis"

Aired: 2017-11-22 02:26:51
More...

Portugal The Man: "Live in the Moment"

Aired: 2017-11-22 02:23:30
More...

Dire Straits: "Calling Elvis"

Aired: 2017-11-22 02:19:19
More...

Little Feat: "Rocket In My Pocket"

Aired: 2017-11-22 02:15:59
More...

Lucinda Williams: "Car Wheels On a Gravel Road"

Aired: 2017-11-22 02:11:04
More...

Govinda: "Inner Membrane"

Aired: 2017-11-22 02:06:35
More...

Mike Zito: "Chip Off The Block"

Aired: 2017-11-22 02:03:02
More...

O.A.R.: "I Go Through"

Aired: 2017-11-22 01:59:19
More...

Jono Manson (local): "The Christian Thing"

Aired: 2017-11-22 01:56:22
More...

David Byrne: "Hanging Upside Down"

Aired: 2017-11-22 01:51:55
More...

Dumpstafunk: "Raise the House"

Aired: 2017-11-22 01:45:27
More...

Ben Harper & The Innocent Cr: "With My Own Two Hands"

Aired: 2017-11-22 01:41:48
More...

Jason Isbell & The 400 Unit: "If We Were Vampires"

Aired: 2017-11-22 01:38:11
More...

Magnetic Fields: "God Wants Us To Wait"

Aired: 2017-11-22 01:36:08
More...

St. Vincent: "Los Ageless"

Aired: 2017-11-22 01:32:21
More...

John Hiatt: "HAVE A LITTLE FAITH IN ME"

Aired: 2017-11-22 01:28:35
More...

Gin Wigmore: "Black Sheep"

Aired: 2017-11-22 01:25:27
More...

Neil Young: "When You Dance You Can Real"

Aired: 2017-11-22 01:21:21
More...

Frightened Rabbit: "How It Gets In (feat. Julie"

Aired: 2017-11-22 01:18:37
More...

Ewert & The Two Dragons: "Good Man Down"

Aired: 2017-11-22 01:14:40
More...

Flow Nice (Local): "Thinking Of You"

Aired: 2017-11-22 01:10:55
More...

Heavy Circles: "Henri (on-ree)"

Aired: 2017-11-22 01:04:58
More...

Chris Isaak: "LET ME DOWN EASY (Single Re"

Aired: 2017-11-22 01:01:21
More...

Coldplay: "Moses"

Aired: 2017-11-22 00:55:57
More...

Jacob Thomas Jr.: "Whiskey Roller Coaster"

Aired: 2017-11-22 00:51:55
More...

Paolo Nutini: "New Shoes"

Aired: 2017-11-22 00:48:35
More...

Chris Rea: "Texas"

Aired: 2017-11-22 00:43:46
More...

repeat, repeat: "Everybody's Falling in Love"

Aired: 2017-11-22 00:40:57
More...

Bob Marley & The Wailers: "One Love / People Get Ready"

Aired: 2017-11-22 00:38:13
More...

Randall Bramblett: "Roll"

Aired: 2017-11-22 00:34:52
More...

Lyle Lovett: "Skinny Legs"

Aired: 2017-11-22 00:31:52
More...

Big Head Todd & Monsters: "Glow"

Aired: 2017-11-22 00:27:51
More...

Counting Crows: "Hanginaround"

Aired: 2017-11-22 00:23:35
More...

k.d. lang: "Sexuality"

Aired: 2017-11-22 00:20:14
More...

Blind Boys of Ala./Bonnie Ra: "When The Spell is Broken"

Aired: 2017-11-22 00:14:59
More...

Adamski: "Time Capsule"

Aired: 2017-11-22 00:09:30
More...

These United States: "Miss Underground"

Aired: 2017-11-22 00:02:41
More...

Lumineers: "Ho Hey"

Aired: 2017-11-22 00:00:12
More...

: ""

Aired: