opencoil-paracity/api_fun/damaged_scooters/fetchnonactivescooters.php

100 lines
3.0 KiB
PHP
Raw Normal View History

2021-02-04 19:22:22 +01:00
<?php
function save_latestData(){
$import_berlin_scooters = file_get_contents('./scooter_UUID.json');
$berlin_scooters = json_decode($import_berlin_scooters, true);
$API_call = 'https://platform.tier-services.io/v1/vehicle/';
$id = 0;
unlink("./damaged_map/nonactiveScooters.json");
fopen("./damaged_map/nonactiveScooters.json", 'w+');
foreach ($berlin_scooters as $berlin_scooter) {
//if ($id < 1000) {
$nonactiveScootersJSON = './damaged_map/nonactiveScooters.json';
$scooterData = callAPI($API_call . $berlin_scooter["UUID"]);
$API_response = json_decode($scooterData, true);
if ($API_response === 'null') {
echo 'api connection failed'.PHP_EOL;
} else {
$scooter_Status = $API_response['data']['attributes']['state'];
if ($scooter_Status === 'ACTIVE') {
$id++;
echo 'scooter ACTIVE'.PHP_EOL;
continue;
} else {
appendJSON($nonactiveScootersJSON, $API_response);
echo 'INACTIVE scooter added'.PHP_EOL;
$id++;
};
};
//} else {
// return;
//}
}
};
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);
}
};
function callAPI($url){
//echo $url;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'x-api-key: bpEUTJEBTf74oGRWxaIcW7aeZMzDDODe1yBoSxi2',
'Content-Type: application/json',
)); // set the api key
curl_setopt($curl, CURLOPT_USERAGENT,'ProductionRelease/3.8.2 (app.tier.sharing; build:3.8.2.0; iOS 12.4.4) Alamofire/4.9.1'); // set user-agent to that of the Tier.app, sniffed by burpsuite
$result = curl_exec($curl);
if(!$result){return ('api connection failed'.PHP_EOL);}
curl_close($curl);
//echo $result;
return $result;
};
save_latestData();
?>