RadioBOSS song request script. An example script showing how to implement song requests in RadioBOSS.
User manual page about song requests: https://manual.djsoft.net/radioboss/en/songrequests.htm
How it works
It shows a form with the three fields: artist, title and message. At least one of the fields (artist or title) have to be filled.
The requested song is searched in the music library. If it's found, the request is added to the song request list in RadioBOSS along with the message entered.
It is strongly recommended to have some web programming knowledge in order to install and use this script.
If a script does't work, you can set the $show_errors to 1 to see the detailed error message. Please do not forget to set it back to 0 once the script is deployed.
Update 2019-11-25
Script uses cURL instead of file_get_contents to communicate with RadioBOSS. This allows the script to work on hosts where file_get_contents is not permitted to access URLs.
User manual page about song requests: https://manual.djsoft.net/radioboss/en/songrequests.htm
How it works
It shows a form with the three fields: artist, title and message. At least one of the fields (artist or title) have to be filled.
The requested song is searched in the music library. If it's found, the request is added to the song request list in RadioBOSS along with the message entered.
It is strongly recommended to have some web programming knowledge in order to install and use this script.
If a script does't work, you can set the $show_errors to 1 to see the detailed error message. Please do not forget to set it back to 0 once the script is deployed.
Update 2019-11-25
Script uses cURL instead of file_get_contents to communicate with RadioBOSS. This allows the script to work on hosts where file_get_contents is not permitted to access URLs.
Code:
<?php
/*
RadioBOSS Song Request demo script
To play the requested songs in RadioBOSS, schedule an event with "playrequestedsong" command:
http://manual.djsoft.net/radioboss/en/scheduler_commands.htm#songrequest
Please make sure the RadioBOSS API is enabled and a password is set:
http://manual.djsoft.net/radioboss/en/remote_controlapi.htm
If RadioBOSS is installed on a server, please make sure the API port (9000 by default) is allowed in firewall.
Home or studio PC:
If RadioBOSS is installed on a home or studio PC, please make sure it has a static IP address.
If a static IP address is not available, a Dynamic DNS address has to be used instead
The IP address (or dynamic DNS address) is entered into the $rb_server variable (please do not include http://)
If a computer is behing a NAT (this is usually the case when a router is used), then API port (9000 by default) has
to be forwarded in router settings - please see the port forwarding documentation for your router.
*/
//---------------//
// CONFIGURATION //
//---------------//
//RadioBOSS API connection details
$rb_server = '127.0.0.1'; //RadioBOSS hostname or IP
$rb_port = '9000'; //RadioBOSS port
$rb_password = '7bNR5UK'; //API password
//music library name, omitting the .xml extension, the library is loaded from "Music library folder" as set in RadioBOSS settings
$rb_library = 'music';
//show detailed error messages (1 - show error details, 0 - show only general error messages)
//IMPORTANT! Make sure this is set to 0 once everything is configured and working to avoid revealing too many details to users!
//Error messages may contain passwords and other sensitive information
//Set this to 1 only if something's not working to get more details
$show_errors = 0;
//-------------------//
// SONG REQUEST FORM //
//-------------- ----//
//API URL base
$rb_api = "http://$rb_server:$rb_port?pass=$rb_password";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>RadioBOSS Song Request demo</title>
<style>
body {
font-family: Tahoma, sans-serif;
font-size: 0.8em;
}
label {
display: block;
margin-bottom: 5pt;
}
</style>
</head>
<body>
<?php
$last_err = ''; //last HTTPGet error message
function HTTPGet($url) {
global $last_err;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$res = curl_exec($curl);
if ($res === false) {
$last_err = curl_error($curl);
}
curl_close($curl);
return $res;
}
function result($msg) {
$back_link = '<a href="javascript:history.back();">Back</a>';
exit("$msg $back_link");
}
$type = isset($_POST['type']) ? $_POST['type'] : '';
if ($type === '') {
echo '<form method="post">
<input type="hidden" name="type" value="request">
<label>Artist
<input size="30" name="artist"></label>
<label>Title
<input size="30" name="title"></label>
<label>Message
<textarea cols="30" rows="3" name="message"></textarea></label>
<button>Request a song</button>
</form>';
} elseif ($type === 'request') {
//requested artist
$artist = mb_strtolower(trim($_POST['artist']));
if ($artist === '') {
$artist = false;
}
//requested title
$title = mb_strtolower(trim($_POST['title']));
if ($title === '') {
$title = false;
}
if (($artist === false) && ($title === false)) {
result('No artist or title entered.');
}
//load library
$library_raw = HTTPGet("$rb_api&action=library&filename=" . urlencode($rb_library));
if ($library_raw === false) {
$err = 'Song request failed: unable to load music library.';
if ($show_errors) {
$err .= ' Error: ' . $last_err;
}
result($err);
}
//parse XML data
$xml = simplexml_load_string($library_raw);
if ($xml === false) {
result('Song request failed: unable to parse music library XML data.');
}
$fn = false;
//search requested song in a music library
foreach ($xml as $x) {
if ($x->getName() !== 'Track') {
continue;
}
$found = (($artist === false) || (mb_strtolower((string)$x['artist']) === $artist)) &&
(($title === false) || (mb_strtolower((string)$x['title']) === $title));
if ($found) {
$fn = (string)$x['filename'];
break;
}
}
//song found, add to requested songs list in RadioBOSS
if ($fn !== false) {
$msg = isset($_POST['message']) ? $_POST['message'] : '';
$res = HTTPGet("$rb_api&action=songrequest&filename=" . urlencode($fn) . '&message=' . urlencode($msg));
if ($res === 'OK') {
result('Song requested successfully!');
} else {
$err = 'An error occurred while adding song request.';
if ($show_errors) {
$err .= ' Error: ' . $last_err;
}
result($err);
}
} else {
result('Requested song not found in the music library.');
}
}
?>
</body>
</html>