// 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
@ -71,11 +70,8 @@ global $scooters; //"import" the global variable into the function's scope
}
// 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
$file = './latestData.json';
file_put_contents($file, $latestData, LOCK_EX);
//TODO make historical json file..
@ -131,7 +127,46 @@ function makeMarker(int $id){ // generate appropriate markers based on latest JS
}
function appendJSON($filename, $data){ // create historical json data for documentation over time
// read the file if present
$handle = @fopen($filename, 'r+');
// create the file if needed
if ($handle === null)
{
$handle = fopen($filename, 'w+');
}
if ($handle)
{
// seek to the end
fseek($handle, 0, SEEK_END);
// are we at the end of is the file empty
if (ftell($handle) > 0)
{
// move back a byte
fseek($handle, -1, SEEK_END);
// add the trailing comma
fwrite($handle, ',', 1);
// add the new json string
fwrite($handle, json_encode($data) . ']');
}
else
{
// write the first event inside an array
fwrite($handle, json_encode(array($data)));
}
// close the handle on the file
fclose($handle);
}
}
//// make date time file------
@ -164,11 +199,6 @@ function makeMarker(int $id){ // generate appropriate markers based on latest JS
// Custom functions
function checkModified($file, $expiration){ //check if file is older than expiration time
}
function callAPI($url){
$curl = curl_init($url);
@ -258,16 +288,40 @@ function callAPI($url){
map.addLayer(Stamen_Toner);
//for each scooter in $scooters array, request status and construct marker
//the magic: for each scooter in $scooters array, request status and construct marker
<?php
$id = 0; //start id
$filename = "./latestData.json"; //latest locations, 1 entry per scooter