// The api key from Scooty's app/account: iPtAHWdOVLEtgkaymXoMHVVg
// Below is the static html to generate the map, it can also be done more dynamically using https://github.com/LuminFire/leaflet-php but that might be overkill
//debugging, enable all error reporting
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
//measure script execution time
$start_time = microtime(true);
$a=1;
// array of scooters, artist, title, description
global $scooters;
@ -21,26 +30,79 @@ $scooters = array(
// for each scooter in array get the json (TODO, for each, safe to file/database?) $scooters is a multidimensional array: $scooter[0][0] = the first data (api url) of the first scooter, $scooter[1][0] = the first data (api url) of the second scooter, $scooter[0][1] = the artist name of the first scooter etcetc.
$id = 0; //get the first scooter for now
//$id = 0; //get the first scooter for now
function saveLatestJSON(){
global $scooters; //"import" the global variable into the function's scope
//TODO if latestData.json = older than ..2 minutes...call the API again
$id = 0; //start id
foreach($scooters as $item) {
$getJSON = callAPI($scooters[$id][0]); //abstract callAPI for neater reuse
// GET RID OF JSON DECODE!
$arr = json_decode($getJSON, true); // decode JSON data to PHP associative array
// access values from the associative array
$lat = $arr["data"]["attributes"]["lat"]; // get lattitude
$lng = $arr["data"]["attributes"]["lng"]; // get longitude
$pos = $lat.', '.$lng; // construct lat/lon for use in leafet
$lst = $arr["data"]["attributes"]["lastLocationUpdate"]; // get time of last position
$rnt = $arr["data"]["attributes"]["isRentable"]; // check if scooter is rentable,
$act = $arr["data"]["attributes"]["state"]; // check if scooter is ACTIVE or MAINTAINANCE
// access values from the multidimensional $scooters array
$nam = $scooters[$id][1]; // get the artist name
$tit = $scooters[$id][2]; // get the works title
$des = $scooters[$id][3]; // get the works description
//construct json with latest data
$latestData[] = array( "ID" => $id,
"lastPos" => $pos,
"lastLocationUpdate" => $lst,
"isRentable" => $rnt,
"state" => $act,
"name" => $nam,
"title" => $tit,
"desc" => $des);
$id++;
}
return $latestData; //return array
}
// THIS ONLY NEEDS TO RUN EVERY 1 or 2 minutes, possibly in the background, als this should construct historical json as per tom martins post here: https://stackoverflow.com/questions/7895335/append-data-to-a-json-file-with-php/7895384
$arrayToEcho = saveLatestJSON();
$latestData = json_encode($arrayToEcho, JSON_FORCE_OBJECT); // force object accepts 0 as a key for the array and not NULL