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

Margaret Burke local: "If God Was a Woman"

Aired: 2024-04-24 15:54:21
More...

Sarah Shook the Disarmers: "Revelations"

Aired: 2024-04-24 15:38:30
More...

Cage the Elephant: "Neon Pill"

Aired: 2024-04-24 15:23:08
More...

Free Range Buddhas (local): "End of the Story"

Aired: 2024-04-24 15:11:15
More...

Nathaniel Rateliff / Night Sweats: "Heartless"

Aired: 2024-04-24 14:58:19
More...

Chicano Batman: "Fly"

Aired: 2024-04-24 14:47:03
More...

Beck: "Girl"

Aired: 2024-04-24 14:43:35
More...

Hurray for the Riff Raff: "Hawkmoon"

Aired: 2024-04-24 14:11:13
More...

Phoebe Bridgers: "Motion Sickness"

Aired: 2024-04-24 14:00:28
More...

MGMT: "Electric Feel"

Aired: 2024-04-24 13:49:16
More...

Tedeschi Trucks Band: "Come See About Me"

Aired: 2024-04-24 13:35:43
More...

Zach Seabaugh: "Christmas Lights"

Aired: 2024-04-24 13:26:27
More...

String Bone: "Wow! Oh Yeah!"

Aired: 2024-04-24 13:17:34
More...

Bomba Estreo: "Deja pron: DAY-hah"

Aired: 2024-04-24 13:05:34
More...

Maya Hawke: "Missing Out"

Aired: 2024-04-24 12:38:24
More...

Daniel Donato: "Hi-Country"

Aired: 2024-04-24 12:26:01
More...

Blondshell: "Docket (feat. Bully)"

Aired: 2024-04-24 12:11:36
More...

Gone Gone Beyond: "Coast (feat. Moontricks)"

Aired: 2024-04-24 12:00:55
More...

Mark Knopfler: "Ahead of the Game"

Aired: 2024-04-24 11:50:43
More...

Sierra Green and the Giants: "Break in the Road"

Aired: 2024-04-24 11:40:41
More...

Sharon Van Etten: "Taking Chances"

Aired: 2024-04-24 11:31:38
More...

Phosphorescent: "Revelator"

Aired: 2024-04-24 11:20:13
More...

Rickie Lee Jones: "Rebel Rebel"

Aired: 2024-04-24 11:09:28
More...

Jade Bird: "Burn the Hard Drive (feat. Mura Masa)"

Aired: 2024-04-24 10:56:53
More...

Big Something: "Wildfire"

Aired: 2024-04-24 10:46:36
More...

Imogen Clark: "The Art of Getting Through"

Aired: 2024-04-24 10:35:19
More...

Lake Street Dive: "Good Together"

Aired: 2024-04-24 10:27:39
More...

Cousteau: "THE LAST GOOD DAY OF THE YEAR"

Aired: 2024-04-24 10:16:46
More...

Postdata: "Try"

Aired: 2024-04-24 10:06:26
More...

Kirsty MacColl: "Us Amazonians"

Aired: 2024-04-24 09:50:23
More...

Bettye LaVette: "Plan B"

Aired: 2024-04-24 09:42:52
More...

Cowboy Junkies: "Sweet Jane"

Aired: 2024-04-24 09:29:36
More...

Sylvan Esso: "Ferris Wheel"

Aired: 2024-04-24 09:15:37
More...

Dessa: "Good for You"

Aired: 2024-04-24 08:56:38
More...

Madi Diaz: "Everything Almost"

Aired: 2024-04-24 08:46:45
More...

Marias, the: "Run Your Mouth"

Aired: 2024-04-24 08:33:40
More...

Cage the Elephant: "Neon Pill"

Aired: 2024-04-24 08:19:44
More...

Swamp Dogg feat. Jenny Lewis: "Count the Days"

Aired: 2024-04-24 08:05:12
More...

Grizfolk: "The Struggle"

Aired: 2024-04-24 07:58:20
More...

Posies: "Golden Blunders"

Aired: 2024-04-24 07:45:10
More...

Charley Crockett: "Trinity River"

Aired: 2024-04-24 07:33:25
More...

Chicano Batman: "Fly"

Aired: 2024-04-24 07:22:27
More...

Eilen Jewell: "You Know My Love"

Aired: 2024-04-24 07:18:08
More...

Khruangbin: "Pon Pn"

Aired: 2024-04-24 07:07:18
More...

Jimi Hendrix: "Castles Made Of Sand"

Aired: 2024-04-24 06:49:00
More...

MUNA: "Number One Fan"

Aired: 2024-04-24 06:34:31
More...

Good Neighbours: "Home"

Aired: 2024-04-24 06:24:21
More...

Budos Band: "Arcane Rambler"

Aired: 2024-04-24 05:59:19
More...

Lone Bellow: "Green Eyes and a Heart of Gold"

Aired: 2024-04-24 05:45:19
More...

Pearl Jam: "Retrograde"

Aired: 2024-04-24 05:13:10
More...

Hurray for the Riff Raff: "Hawkmoon"

Aired: 2024-04-24 05:02:50
More...

The Last Dinner Party: "The Feminine Urge"

Aired: 2024-04-24 04:49:28
More...

Lenny Kravitz: "Human"

Aired: 2024-04-24 04:28:36
More...

Blues Traveler: "Runaround"

Aired: 2024-04-24 04:17:09
More...

Lemonheads: "The Outdoor Type"

Aired: 2024-04-24 04:07:29
More...

Charley Crockett: "$10 Cowboy"

Aired: 2024-04-24 03:57:35
More...

Michael Marcagi: "Scared to Start"

Aired: 2024-04-24 03:47:36
More...

Tom Petty: "I Need to Know"

Aired: 2024-04-24 03:41:01
More...

Del Amitri: "TELL HER THIS"

Aired: 2024-04-24 03:30:54
More...

Bob Marley the Wailers: "Three Little Birds"

Aired: 2024-04-24 03:22:12
More...

Broken Bells: "Good Luck"

Aired: 2024-04-24 03:11:42
More...

Squirrel Nut Zippers: "Hell"

Aired: 2024-04-24 03:01:01
More...

Ponderosa: "Navajo"

Aired: 2024-04-24 02:48:00
More...

Phish: "Blaze On"

Aired: 2024-04-24 02:43:42
More...

Strumbellas: "Salvation"

Aired: 2024-04-24 02:32:18
More...

Beck: "Dreams"

Aired: 2024-04-24 02:19:02
More...

Dave Matthews (Solo): "Save Me"

Aired: 2024-04-24 02:07:45
More...

Jade Bird: "Burn the Hard Drive (feat. Mura Masa)"

Aired: 2024-04-24 01:57:29
More...

Paula Cole: "The Replacements and Dinosaur Jr."

Aired: 2024-04-24 01:43:47
More...

Maya Hawke: "Missing Out"

Aired: 2024-04-24 01:28:39
More...

Glen Hansard: "Winning Streak"

Aired: 2024-04-24 01:16:15
More...

Mama Zu: "Lip"

Aired: 2024-04-24 01:06:08
More...

Iron Wine: "All In Good Time (feat. Fiona Apple)"

Aired: 2024-04-24 00:53:33
More...

Nathaniel Rateliff: "Redemption"

Aired: 2024-04-24 00:43:22
More...

Adele: "Send My Love (To Your New Love)"

Aired: 2024-04-24 00:32:57
More...

Grace Potter The Nocturnals: "Paris (Ooh La La)"

Aired: 2024-04-24 00:14:32
More...

Lewis Taylor: "Lovelight"

Aired: 2024-04-24 00:00:58
More...

: ""

Aired: