Created
June 28, 2018 20:48
-
-
Save anthonyeden/189ecfb88b9e426b3c990206d7dbd12a to your computer and use it in GitHub Desktop.
MetaRadio: Simple PHP Now Playing Script (Example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This script receives data from MetaRadio's HTTP Output module | |
// This is a simple example. Feel free to make it your own. | |
// https://mediarealm.com.au/metaradio/ | |
// Specify a secret username and password | |
$username = "---SECRET-USERNAME---"; | |
$password = "---PASSWORD-USERNAME---"; | |
// Where to store the now-playing data on this web server? | |
$filename = "metaradio.json"; | |
// No need to edit below this line (unless you want to) | |
// ********************************************************* | |
header('X-Powered-By: MetaRadio'); | |
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_USER'] !== $username || $_SERVER['PHP_AUTH_PW'] !== $password) { | |
header('WWW-Authenticate: Basic realm="MetaRadio-Input"'); | |
header('HTTP/1.0 401 Unauthorized'); | |
echo 'Please supply a username and password via HTTP Basic Authentication'; | |
die(); | |
} | |
$fp = fopen('vardump.txt', 'w'); | |
fwrite($fp, serialize($_POST)); | |
fclose($fp); | |
// Check for supplied data | |
if(!isset($_POST['title']) || !isset($_POST['artist']) || empty($_POST['title']) || empty($_POST['artist'])) { | |
die("ERROR: Title & Artist have not been supplied"); | |
} | |
// Get the data and put it into an array | |
$data = array( | |
"Now" => array( | |
"Title" => trim($_POST['title']), | |
"Artist" => trim($_POST['artist']), | |
"Album" => trim($_POST['album']), | |
"DurationSecs" => (Integer)trim($_POST['durationsecs']), | |
"PlayoutID" => trim($_POST['playoutid']), | |
"ItemCode" => trim($_POST['itemcode']), | |
"DateReceived" => date("Y-m-d H:i:s"), | |
) | |
); | |
// Save the array to a JSON file | |
$fp = fopen($filename, 'w'); | |
fwrite($fp, json_encode($data)); | |
fclose($fp); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This script displays the data received by 'metaradio_input.php' | |
// This is a simple example to display now-playing data. Feel free to make it your own. | |
// https://mediarealm.com.au/metaradio/ | |
// Where is the now-playing data stored on this web server? | |
$filename = "metaradio.json"; | |
// Show this text whenever there's no track currently playing | |
$fallbacktext = "Playing the greatest hits!"; | |
// No need to edit below this line (unless you want to) | |
// ********************************************************* | |
if(basename(__FILE__) == basename($_SERVER["SCRIPT_FILENAME"])) | |
header('X-Powered-By: MetaRadio'); | |
if(!file_exists($filename)) { | |
echo "ERROR: File " . $filename . " does not exist."; | |
} else { | |
// Open the file | |
$fp = fopen($filename, 'r'); | |
$data = json_decode(fread($fp, filesize($filename)), true); | |
if(isset($data['Now']['DateReceived'])) { | |
// Calculate the end-time of the current 'now playing' file | |
$endTime = strtotime($data['Now']['DateReceived']) + $data['Now']['DurationSecs']; | |
if($endTime >= time()) { | |
// Item hasn't ended already - show the data | |
$now = "Now Playing: " . $data['Now']['Title'] . " - " . $data['Now']['Artist']; | |
} | |
} | |
if(isset($now)) { | |
echo $now; | |
} else { | |
echo $fallbacktext; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment