Я получаю идентификатор от MySQL, а $ id запускает указанный c URL, содержащий идентификатор $.
$Ortungsnummer = $row['Ortungs_Identifikation'];
$json = file_get_contents('http://www.profi-ortung.de/api/v1/user/devices/?access_token=');
$arr = json_decode($json,true);
$arr=json_decode($json);
foreach($arr as $obj) {
$device_id = $obj->device_id;
$latest_signal_id = $obj->latest_signal_id;
if ($device_id == $Ortungsnummer) {
}
}
$ip= "$Ortungsnummer";
$signal = "$latest_signal_id";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.profi-ortung.de/api/v1/device/$ip/signal/$signal/?access_token=");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$outputJson = curl_exec($ch);
$arr2 = json_decode($outputJson);
$fahrzeug_lat= "$arr2->dec_lat";
$fahrzeug_long= "$arr2->dec_long";
$start_lat ="52.2";
$start_long = "8.9";
function distanceCalculation($point1_lat, $point1_long, $point2_lat, $point2_long, $unit = 'km', $decimals = 2) {
$degrees = rad2deg(acos((sin(deg2rad($point1_lat))*sin(deg2rad($point2_lat))) + (cos(deg2rad($point1_lat))*cos(deg2rad($point2_lat))*cos(deg2rad($point1_long-$point2_long)))));
switch($unit) {
case 'km':
$distance = $degrees * 111.13384;
case 'mi':
$distance = $degrees * 69.05482;
break;
case 'nmi':
$distance = $degrees * 59.97662; nautical miles)
}
return round($distance, $decimals);
}
$point1 = array("lat" => $fahrzeug_lat, "long" => $fahrzeug_long);
$point2 = array("lat" => $start_lat, "long" => $start_long);
$km = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);
$mi = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long'], 'mi');
$nmi = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long'], 'nmi');
echo $km;
Когда я повторяю его, я получаю данные о том, сколько $ id есть! До сих пор код работает! Теперь я хочу запустить код, который вычисляет расстояние.
Проблема в том, что он вычисляет расстояние только для первого результата, поэтому только для первого $ id, но $fahrzeug_lat= "$arr2->dec_lat";
и $fahrzeug_long= "$arr2->dec_long";
имеют оба данных в нем.
Не знаю, как решить проблему, пожалуйста, помогите!
Пример данных для $ JSON:
[
{
"is_disable": false,
"canbus_id": "",
"daily_mileage": "124.67",
"command_internal_number": "",
"tank_height": "0",
"immobilizer_status": 0,
"garmin_check_driver": 0,
"velocity_info": 0,
"latest_mileage": "239076.8264",
"temp_min": 0,
"sim_number": "89490200001222280318",
"canbus_serial": "",
"deleted_at": "None",
"sap_status": 1,
"user_id": 152620,
"latest_engine_hour": 0,
"tank_capacity": null,
"temp_max": 0,
"gsm_number": "76140169",
"beacon_id": 357454070966716,
"note": "",
"canbus_program_nr": "",
"activation": 0,
"car_state": "",
"serial_number": "07224812",
"type": 75,
"price": "13.90",
"added_by": 30,
"latest_signal_id": 11375391888,
"gsm_areacode": "0151",
"tank_capacity_lt": 0,
"latest_zone_id": -1,
"hide_status": false,
"latest_geofence_signal_id": 11375383116,
"trip_status": 0,
"added_at": "2016-08-11 17:12:48",
"device_id": 1000016558,
"name": "100",
"transmission": "94.5746676141",
"box_type": 22,
"start_date": "None",
"latest_task_record_id": 0,
"command_password": "",
"canbus_type": "",
"latest_geocode_sent_at": "2020-07-09 15:52:40"
},
{
"is_disable": false,
"canbus_id": "",
"daily_mileage": "233.28",
"command_internal_number": "",
"tank_height": "0",
"immobilizer_status": 0,
"garmin_check_driver": 0,
"velocity_info": 0,
"latest_mileage": "226282.657",
"temp_min": 0,
"sim_number": "89490200001222287305",
"canbus_serial": "",
"deleted_at": "None",
"sap_status": 1,
"user_id": 152620,
"latest_engine_hour": 0,
"tank_capacity": null,
"temp_max": 0,
"gsm_number": "76098470",
"beacon_id": 356173068452528,
"note": "",
"canbus_program_nr": "",
"activation": 0,
"car_state": "",
"serial_number": "06652243",
"type": 120,
"price": "13.90",
"added_by": 30,
"latest_signal_id": 11375392298,
"gsm_areacode": "0151",
"tank_capacity_lt": 0,
"latest_zone_id": -1,
"hide_status": false,
"latest_geofence_signal_id": 11375379385,
"trip_status": 0,
"added_at": "2016-07-18 13:17:22",
"device_id": 1000014684,
"name": "101",
"transmission": "104.879623019",
"box_type": 22,
"start_date": "None",
"latest_task_record_id": 0,
"command_password": "",
"canbus_type": "",
"latest_geocode_sent_at": "2020-07-09 15:52:43"
}
]
Пример данных для $ JSON2:
{"canbus_engine_temp": "", "signal_id": 11375391888, "input_2_voltage": 0.0, "canbus_doors": "", "nr_sat": 18, "signal_strenght": 18, "canbus_fuel_consumption": "", "beacon_id": 357454070966716, "input_6": 0, "input_7": 0, "input_4": 0, "input_5": 0, "input_2": 0, "input_3": 0, "input_0": 0, "input_1": 1, "canbus_total_driving_time": "", "dec_long": 8.9, "canbus_fuel_level_pr": "", "mileage": 239076.8281, "canbus_lights": "", "canbus_device_state": "", "canbus_total_engine_hours": "", "canbus_fuel_level_lt": "", "canbus_total_fuel_used": "", "dec_lat": 52.3, "input_1_voltage": 14.02, "speed_km": 45.0, "canbus_info_indicators": "", "geocode_sent_at": "2020/07/09 15:52:40", "added_at": "2020-07-09 15:52:43", "signal_mode": 0, "canbus_fuel_range": "", "canbus_total_idle_fuel_use": "", "signal_type": 5, "canbus_engine_rpm": ""}
или
{"canbus_engine_temp": "", "signal_id": 11375268853, "input_2_voltage": 0.0, "canbus_doors": "", "nr_sat": 17, "signal_strenght": 18, "canbus_fuel_consumption": "", "beacon_id": 356173068452528, "input_6": 0, "input_7": 0, "input_4": 0, "input_5": 0, "input_2": 0, "input_3": 0, "input_0": 0, "input_1": 1, "canbus_total_driving_time": "", "dec_long": 9.0, "canbus_fuel_level_pr": "", "mileage": 226267.2188, "canbus_lights": "", "canbus_device_state": "", "canbus_total_engine_hours": "", "canbus_fuel_level_lt": "", "canbus_total_fuel_used": "", "dec_lat": 52.2, "input_1_voltage": 13.95, "speed_km": 10.0, "canbus_info_indicators": "", "geocode_sent_at": "2020/07/09 15:36:16", "added_at": "2020-07-09 15:36:23", "signal_mode": 0, "canbus_fuel_range": "", "canbus_total_idle_fuel_use": "", "signal_type": 5, "canbus_engine_rpm": ""}
Ortungsnummer для первого $ JSON2
1000014684
Ortungsnummer для второго $ JSON2
1000016558