Browse Source

check if latestjson is younger than 60s, else update curl.php

master
user 4 years ago
parent
commit
7838aee055
  1. 35
      website/curl.php

35
website/curl.php

@ -27,7 +27,6 @@ $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. // 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
@ -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 // 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.. //TODO make historical json file..
@ -164,11 +160,6 @@ function makeMarker(int $id){ // generate appropriate markers based on latest JS
// Custom functions // Custom functions
function checkModified($file, $expiration){ //check if file is older than expiration time
}
function callAPI($url){ function callAPI($url){
$curl = curl_init($url); $curl = curl_init($url);
@ -260,14 +251,34 @@ function callAPI($url){
//for each scooter in $scooters array, request status and construct marker //for each scooter in $scooters array, request status and construct marker
<?php <?php
$filename = "./latestData.json";
// check how old the latest json is
if (time()-filemtime($filename) > 60) {
$arrayToEcho = saveLatestJSON();
$latestData = json_encode($arrayToEcho, JSON_FORCE_OBJECT); // force object accepts 0 as a key for the array and not NULL
file_put_contents($filename, $latestData, LOCK_EX);
$id = 0; //start id $id = 0; //start id
foreach($scooters as $item) { foreach($scooters as $item) {
// echo makeMarker($id);
echo makeMarker($id); echo makeMarker($id);
$id++;
}
} else { // file younger than 60 seconds
$id = 0; //start id
foreach($scooters as $item) {
echo makeMarker($id);
$id++; $id++;
} }
}
?> ?>
// time slider // time slider

Loading…
Cancel
Save