[Web site] Song request demo script

djsoft

Well-known member
Staff member
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.

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>
 

Attachments

thanks for the contribution, I have a doubt, the code is pasted as is in the example? clear changing the ip the port and the key
 
Yes, this is an example. The password is an example too - it won't work anywhere.
 
This script will not work for me. I enabled an API in the RadioBOSS settings, set a password, and all the data entered in the script. I've made a database with a complete music called music.xml. In the script under the database is already written by music. The dynamic dns is working properly, the ip address of the computer on which RadioBOSS is located is located in the DMZ zone on the router. When I open a script on a server and enter the artist name, I get a note that I can not load the database. In the settings at one place, NVDA read \ database \. I guess it's a path to the base. My databases are in appdata \ roaming \ djsoftnet \ radiobossxxx \ database.
 
Sasa Grbic said:
The dynamic dns is working properly, the ip address of the computer on which RadioBOSS is located is located in the DMZ zone on the router. When I open a script on a server and enter the artist name, I get a note that I can not load the database.
Can you please post the exact error message?
 
Sasa Grbic said:
Code:
Song request failed: unable to load music library.
It appears that either dynamic IP does not work, or song request script is not configured properly. Please try accessing from your browser (change address and port to actual values).
http://address:port?pass=1

It should show you the password is incorrect - that means, the request reached RadioBOSS and was processed. If you see some other error message, then RadioBOSS is inaccessible from internet.
 
When I entered the web browser http://name.server.com:port/?pass=xxxxxxxxxxxxxxxxx I got the following code:
Code:
 E003: Nothing to do
 
Please make sure that you did not include "http" in the server address when configuring a script.

Correct:
$rb_server = 'name.server.com';

Wrong:
$rb_server = 'http://name.server.com';
 
It's hard to tell what's wrong. You can send your script to support@djsoft.net, we'll check what could be wrong there. Alternatively, if you are familiar with PHP, you may add some additional error checking to the script to see why it fails.
 
stokemusic said:
Could a script be incorporated into the next release of RB which could do this automatically if desired?
The script has to be installed on your web site, and you can always get the updated version here.
 
stokemusic said:
yes I understand that. The trouble is the script provided here doesn't seem to work, either for me or anyone else.
The script is pretty simple and, if configured correctly it will work. You also need to make sure that it's able to communicate with RadioBOSS. If it doesn't work for you, please contact us at support@djsoft.net, and attach the script you use.
 
You can make a video demonstration, because I do not know anything and I do not know how it is done, it would be so kind to resign or some soul that understands how it is done, to make a video for those who do not understand how it is done please, thank you
 
diabolix said:
You can make a video demonstration, because I do not know anything and I do not know how it is done, it would be so kind to resign or some soul that understands how it is done, to make a video for those who do not understand how it is done please, thank you
This requires some (little) web programming knowledge. This topic is huge so it can't be covered here.
The general idea is that you modify access parameters in the script so it connects with your RadioBOSS installation, and then upload the script to your web site. 
 
Back
Top