I have created an alternative request script for anyone who wants to make use of it. This one makes use of just one box to search for Artist or Song Title. It will also check the file name if a file has not been tagged properly. It then returns a drop down box, whereby if a song has been covered by several artists, the user can select the version they want to hear. I have omitted the message box from the search as my radio is almost 100% automated and I don't want un-moderated messages appearing on the site. I'm sure someone can amend the script to include it if they need it.
<?php
// Config variables
$rb_server = 'Your RadioBOSS server address'; // Your RadioBOSS server address
$rb_port = '9000'; // RadioBOSS port (9000 is the default one
$rb_password = 'Your password'; // API password
$rb_library = 'Music'; // Music library file (without .xml). Your Music Library name
// API base URL
$rb_api = "http://$rb_server:$rb_port?pass=$rb_password";
// HTTP get function
function HTTPGet($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
// Function to process the song request
function result($msg) {
echo "$msg <a href='javascript:history.back();'>Back</a>";
exit;
}
// Handling the search and request process
$type = isset($_POST['type']) ? $_POST['type'] : '';
if ($type === '') {
echo '<form method="post">
<input type="hidden" name="type" value="search">
<label>Search by Artist or Title:</label>
<input type="text" name="search" placeholder="Artist or Title">
<button type="submit">Search</button>
</form>';
} elseif ($type === 'search') {
$search = mb_strtolower(trim($_POST['search']));
if ($search === '') {
result('No search term entered.');
}
// Load library data from RadioBOSS API
$library_raw = HTTPGet("$rb_api&action=library&filename=" . urlencode($rb_library));
if ($library_raw === false) {
result('Song request failed: unable to load music library.');
}
// Parse the XML data
//$xml = simplexml_load_string($library_raw);
// Use LIBXML_PARSEHUGE to bypass restrictions
// This allows parsing huge XML files but requires PHP 5.3+.
$xml = simplexml_load_string($library_raw, "SimpleXMLElement", LIBXML_PARSEHUGE);
if ($xml === false) {
result('Song request failed: unable to parse music library XML data.');
}
// Search for matching artist or title
$matches = [];
foreach ($xml->Track as $track) {
$artist = mb_strtolower((string)$track['artist']);
$title = mb_strtolower((string)$track['title']);
if (strpos($artist, $search) !== false || strpos($title, $search) !== false) {
$matches[] = [
'artist' => (string)$track['artist'],
'title' => (string)$track['title'],
'filename' => (string)$track['filename']
];
}
}
if (count($matches) > 0) {
echo '<form method="post">
<input type="hidden" name="type" value="request">
<label>Choose a song:</label>
<select name="song">';
foreach ($matches as $match) {
echo '<option value="' . $match['filename'] . '">' . $match['artist'] . ' - ' . $match['title'] . '</option>';
}
echo '</select><br><button type="submit">Request this song</button></form>';
} else {
result('No matches found for your search.');
}
} elseif ($type === 'request') {
// Handle song request
$filename = $_POST['song'];
$msg = isset($_POST['message']) ? $_POST['message'] : '';
// Send song request to RadioBOSS API
$res = HTTPGet("$rb_api&action=songrequest&filename=" . urlencode($filename) . '&message=' . urlencode($msg));
if ($res === 'OK') {
result('Song requested successfully!');
} else {
result('An error occurred while adding song request.');
}
}
?>
<?php
// Config variables
$rb_server = 'Your RadioBOSS server address'; // Your RadioBOSS server address
$rb_port = '9000'; // RadioBOSS port (9000 is the default one
$rb_password = 'Your password'; // API password
$rb_library = 'Music'; // Music library file (without .xml). Your Music Library name
// API base URL
$rb_api = "http://$rb_server:$rb_port?pass=$rb_password";
// HTTP get function
function HTTPGet($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
// Function to process the song request
function result($msg) {
echo "$msg <a href='javascript:history.back();'>Back</a>";
exit;
}
// Handling the search and request process
$type = isset($_POST['type']) ? $_POST['type'] : '';
if ($type === '') {
echo '<form method="post">
<input type="hidden" name="type" value="search">
<label>Search by Artist or Title:</label>
<input type="text" name="search" placeholder="Artist or Title">
<button type="submit">Search</button>
</form>';
} elseif ($type === 'search') {
$search = mb_strtolower(trim($_POST['search']));
if ($search === '') {
result('No search term entered.');
}
// Load library data from RadioBOSS API
$library_raw = HTTPGet("$rb_api&action=library&filename=" . urlencode($rb_library));
if ($library_raw === false) {
result('Song request failed: unable to load music library.');
}
// Parse the XML data
//$xml = simplexml_load_string($library_raw);
// Use LIBXML_PARSEHUGE to bypass restrictions
// This allows parsing huge XML files but requires PHP 5.3+.
$xml = simplexml_load_string($library_raw, "SimpleXMLElement", LIBXML_PARSEHUGE);
if ($xml === false) {
result('Song request failed: unable to parse music library XML data.');
}
// Search for matching artist or title
$matches = [];
foreach ($xml->Track as $track) {
$artist = mb_strtolower((string)$track['artist']);
$title = mb_strtolower((string)$track['title']);
if (strpos($artist, $search) !== false || strpos($title, $search) !== false) {
$matches[] = [
'artist' => (string)$track['artist'],
'title' => (string)$track['title'],
'filename' => (string)$track['filename']
];
}
}
if (count($matches) > 0) {
echo '<form method="post">
<input type="hidden" name="type" value="request">
<label>Choose a song:</label>
<select name="song">';
foreach ($matches as $match) {
echo '<option value="' . $match['filename'] . '">' . $match['artist'] . ' - ' . $match['title'] . '</option>';
}
echo '</select><br><button type="submit">Request this song</button></form>';
} else {
result('No matches found for your search.');
}
} elseif ($type === 'request') {
// Handle song request
$filename = $_POST['song'];
$msg = isset($_POST['message']) ? $_POST['message'] : '';
// Send song request to RadioBOSS API
$res = HTTPGet("$rb_api&action=songrequest&filename=" . urlencode($filename) . '&message=' . urlencode($msg));
if ($res === 'OK') {
result('Song requested successfully!');
} else {
result('An error occurred while adding song request.');
}
}
?>