PHP 用 JSON 计算的脚本
PHP Script for Calculating with JSON
我从 MySQL 获得一个 ID,$ id 运行 是一个包含 $ id 的特定 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": ""}
前$JSON2
的Ortungsnummer
1000014684
第二个 $JSON2
Ortungsnummer
1000016558
试试这个:
<?php
$sample_json = '[{
"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": ""
}
]';
//print_r(json_decode($sample_json));
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;
}
return round($distance, $decimals);
}
$sample_array = json_decode($sample_json);
for($i=0;$i<sizeof($sample_array);$i++){
$fahrzeug_lat= $sample_array[$i]->dec_lat;
$fahrzeug_long= $sample_array[$i]->dec_long;
$start_lat ="52.2";
$start_long = "8.9";
$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 $i.". KM: ".$km; echo "\n";
}
?>
我从 MySQL 获得一个 ID,$ id 运行 是一个包含 $ id 的特定 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": ""}
前$JSON2
的Ortungsnummer1000014684
第二个 $JSON2
Ortungsnummer1000016558
试试这个:
<?php
$sample_json = '[{
"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": ""
}
]';
//print_r(json_decode($sample_json));
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;
}
return round($distance, $decimals);
}
$sample_array = json_decode($sample_json);
for($i=0;$i<sizeof($sample_array);$i++){
$fahrzeug_lat= $sample_array[$i]->dec_lat;
$fahrzeug_long= $sample_array[$i]->dec_long;
$start_lat ="52.2";
$start_long = "8.9";
$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 $i.". KM: ".$km; echo "\n";
}
?>