<?php
header
('Content-Type: application/json');

$data = array();

//what's the current time?
$current_time date("U");

//format the date / time to how Radio NZ names their file
$time_string date("Ymd-H00"$current_time);
$filename $time_string "-048.mp3";
$file_url "http://on-demand.radionz.co.nz/news/" $filename;

if(
UR_exists($file_url))
{
    
//it's ok, use this
} else {
    
//If the news for this hour hasn't been posted yet, see if the bulletin for the hour before exists
    
$current_time -= 3600;
    
$time_string date("Ymd-H00"$current_time);
    
$filename $time_string "-048.mp3";
    
$file_url "http://on-demand.radionz.co.nz/news/" $filename;
    if(!
UR_exists($file_url))
    {
        
//that doesn't exist either (maybe they changed the naming, maybe they're on to us!)
        //we could give an error message. Alexa will say it's offline anyway
        
die;
    }
}

  
//format the update time as UTC like Amazon expects
  
$utc_time $current_time date("Z");
  
$update_time date("Y-m-d"$utc_time) . "T" date("H:i:s"$utc_time) . ".0Z";

  
$data["uid"] = "000002";
  
$data["updateDate"] = $update_time;
  
$data["titleText"] = "Radio New Zealand News";
  
$data["mainText"] = "";
  
$data["streamUrl"] = $file_url;
  
$data["redirectionUrl"] = "http://www.radionz.co.nz/";

echo 
json_encode($data);

function 
UR_exists($url){
   
$headers=get_headers($url);
   return 
stripos($headers[0],"200 OK")?true:false;
}
?>