The official source code for Serenum API. https://api.serenum.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

1232 lines
54 KiB

<?php
require_once 'class-moonphase.php';
require_once 'class-suncalc.php';
require_once 'class-geotimezone.php';
require_once 'functions.php';
header('Content-Type: application/json;charset=utf-8');
$arr_aqi = [
1 => 'Good',
2 => 'Fair',
3 => 'Moderate',
4 => 'Poor',
5 => 'Very poor'
];
$apikey = (!isset($_GET['key']) ? '[your-api-key]' : strip_tags(htmlspecialchars($_GET['key'])));
$defined_useragent = 'Serenum_serenum.org';
$default_latitude = 59.38079;
$default_longitude = 13.50239;
$get_coordinates = (!isset($_GET['coor']) ? null : strip_tags(htmlspecialchars($_GET['coor'])));
$get_language = 'en';
$get_exclude = (!isset($_GET['exclude']) ? null : strip_tags(htmlspecialchars($_GET['exclude'])));
$get_diff_c1 = (!isset($_GET['c1']) ? null : strip_tags(htmlspecialchars($_GET['c1'])));
$get_diff_c2 = (!isset($_GET['c2']) ? null : strip_tags(htmlspecialchars($_GET['c2'])));
$arr_excluded = explode(',', $get_exclude);
$filename_path = explode('/', $_SERVER['PHP_SELF']);
$filename = $filename_path[count($filename_path) - 1];
$example_path = ($filename == 'index.php' ? null : $filename);
if($get_coordinates == null) {
$arr = [
"id" => 110,
"message" => "Geographical coordinates are missing.",
"example" => "?coor=".$default_latitude.",".$default_longitude,
"example_path" => "/".$example_path."?coor=".$default_latitude.",".$default_longitude
];
} elseif(!preg_match('/^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$/', $get_coordinates)) {
$arr = [
"id" => 140,
"message" => "Geographical coordinates not valid.",
"example" => "?coor=".$default_latitude.",".$default_longitude,
"example_path" => "/".$example_path."?coor=".$default_latitude.",".$default_longitude
];
} elseif(empty($apikey) OR $apikey == '[your-api-key]') {
$arr = [
"id" => 150,
"message" => "API key from OpenWeatherMap are missing.",
"example" => MD5('this-is-just-an-example'),
"example_path" => "/".$example_path."?coor=".$default_latitude.",".$default_longitude."&key=".MD5('this-is-just-an-example')
];
} elseif(!preg_match('/([a-z0-9]{32})$/', $apikey)) {
$arr = [
"id" => 160,
"message" => "The API key are not valid.",
"example" => MD5('this-is-just-an-example'),
"example_path" => "/".$example_path."?coor=".$default_latitude.",".$default_longitude."&key=".MD5('this-is-just-an-example')
];
} elseif($get_exclude != null AND
!in_array('place', $arr_excluded) AND
!in_array('pollen', $arr_excluded) AND
!in_array('airp', $arr_excluded) AND
!in_array('astronomy', $arr_excluded)) {
$arr = [
"id" => 170,
"message" => "Variables are missing.",
"example" => "&exclude=pollen,airp",
"example_path" => "/".$example_path."?coor=".$default_latitude.",".$default_longitude."&exclude=pollen,airp"
];
} else {
# Extract latitude and longitude from the coordinates
list($latitude, $longitude) = explode(',', $get_coordinates);
# Get timezone based on position
$timezone = new ConvertGeolocationToTimezone();
$timezone = $timezone->getNearestTimezone($latitude, $longitude);
$datetime = new DateTime(date('Y-m-d H:i:s'));
$datetime->setTimeZone(new DateTimeZone($timezone));
$timestamp = strtotime($datetime->format('Y-m-d H:i:s'));
# Information about the moon
$astro_moon = new Solaris\MoonPhase();
# Information about the Sun and the Moon
$astro = new AurorasLive\SunCalc($datetime, $latitude, $longitude);
$astro_sun_times = $astro->getSunTimes();
$astro_sun_position = $astro->getSunPosition();
$astro_moon_position = $astro->getMoonPosition($datetime);
$astro_moon_illumination = $astro->getMoonIllumination($datetime);
$astro_moon_times = $astro->getMoonTimes($datetime);
$astro_sun_altitude = $astro_sun_position->altitude * 180 / M_PI;
$astro_sun_azimuth = $astro_sun_position->azimuth * 180 / M_PI;
$astro_sun_rise = ($astro_sun_times['sunrise'] == null ? null : strtotime($astro_sun_times['sunrise']->format('Y-m-d H:i:s')));
$astro_sun_riseend = ($astro_sun_times['sunriseEnd'] == null ? null : strtotime($astro_sun_times['sunriseEnd']->format('Y-m-d H:i:s')));
$astro_sun_goldenhourend = ($astro_sun_times['goldenHourEnd'] == null ? null : strtotime($astro_sun_times['goldenHourEnd']->format('Y-m-d H:i:s')));
$astro_sun_solarnoon = ($astro_sun_times['solarNoon'] == null ? null : strtotime($astro_sun_times['solarNoon']->format('Y-m-d H:i:s')));
$astro_sun_goldenhour = ($astro_sun_times['goldenHour'] == null ? null : strtotime($astro_sun_times['goldenHour']->format('Y-m-d H:i:s')));
$astro_sun_setstart = ($astro_sun_times['sunsetStart'] == null ? null : strtotime($astro_sun_times['sunsetStart']->format('Y-m-d H:i:s')));
$astro_sun_set = ($astro_sun_times['sunset'] == null ? null : strtotime($astro_sun_times['sunset']->format('Y-m-d H:i:s')));
$astro_sun_dusk = ($astro_sun_times['dusk'] == null ? null : strtotime($astro_sun_times['dusk']->format('Y-m-d H:i:s')));
$astro_sun_nauticaldusk = ($astro_sun_times['nauticalDusk'] == null ? null : strtotime($astro_sun_times['nauticalDusk']->format('Y-m-d H:i:s')));
$astro_sun_night = ($astro_sun_times['night'] == null ? null : strtotime($astro_sun_times['night']->format('Y-m-d H:i:s')));
$astro_sun_nadir = ($astro_sun_times['nadir'] == null ? null : strtotime($astro_sun_times['nadir']->format('Y-m-d H:i:s')));
$astro_sun_nightend = ($astro_sun_times['nightEnd'] == null ? null : strtotime($astro_sun_times['nightEnd']->format('Y-m-d H:i:s')));
$astro_sun_nauticaldawn = ($astro_sun_times['nauticalDawn'] == null ? null : strtotime($astro_sun_times['nauticalDawn']->format('Y-m-d H:i:s')));
$astro_sun_dawn = ($astro_sun_times['dawn'] == null ? null : strtotime($astro_sun_times['dawn']->format('Y-m-d H:i:s')));
$astro_sun_isup = ($astro_sun_altitude < 0 ? false : true);
$astro_moon_altitude = $astro_moon_position->altitude * 180 / M_PI;
$astro_moon_azimuth = $astro_moon_position->azimuth * 180 / M_PI;
$astro_moon_rise = (!isset($astro_moon_times['moonrise']) ? null : strtotime($astro_moon_times['moonrise']->format('Y-m-d H:i:s')));
$astro_moon_set = (!isset($astro_moon_times['moonset']) ? null : strtotime($astro_moon_times['moonset']->format('Y-m-d H:i:s')));
$astro_moon_age = $astro_moon->get('age');
$astro_moon_phase = $astro_moon_illumination['phase'];
$astro_moon_phase_name = $astro_moon->phase_name();
$astro_moon_fraction = $astro_moon_illumination['fraction'];
$astro_moon_angle = $astro_moon_illumination['angle'];
$astro_moon_distance_earth = $astro_moon->get('distance');
$astro_moon_distance_sun = $astro_moon->get('sundistance');
$astro_moon_diameter_moon = $astro_moon->get('diameter');
$astro_moon_diameter_sun = $astro_moon->get('sundiameter');
$astro_moon_alwaysup = (isset($astro_moon_times['alwaysUp']) ? true : false);
$astro_moon_alwaysdown = (isset($astro_moon_times['alwaysDown']) ? true : false);
$astro_moon_new = $astro_moon->get_phase('new_moon');
$astro_moon_nextnew = $astro_moon->get_phase('next_new_moon');
$astro_moon_full = $astro_moon->get_phase('full_moon');
$astro_moon_nextfull = $astro_moon->get_phase('next_full_moon');
$astro_moon_firstquarter = $astro_moon->get_phase('first_quarter');
$astro_moon_nextfirstquarter = $astro_moon->get_phase('next_first_quarter');
$astro_moon_lastquarter = $astro_moon->get_phase('last_quarter');
$astro_moon_nextlastquarter = $astro_moon->get_phase('next_last_quarter');
$http_options = ['http' => ['method' => 'GET', 'header' => 'User-Agent: '.$defined_useragent]];
$http_options = stream_context_create($http_options);
$data_geocoding = (in_array('place', $arr_excluded) ? null : json_decode(@file_get_contents('https://nominatim.openstreetmap.org/reverse?lat='.$latitude.'&lon='.$longitude.'&format=json&accept-language='.$get_language, false, $http_options)));
$data_weather = json_decode(@file_get_contents('https://api.openweathermap.org/data/2.5/onecall?lat='.$latitude.'&lon='.$longitude.'&appid='.$apikey.'&units=metric&lang='.$get_language, false, $http_options));
$data_airpollution = (in_array('airp', $arr_excluded) ? null : json_decode(@file_get_contents('https://api.openweathermap.org/data/2.5/air_pollution?lat='.$latitude.'&lon='.$longitude.'&appid='.$apikey, false, $http_options)));
$timezone_offset = (!isset($data_weather->timezone_offset) ? null : $data_weather->timezone_offset);
$geo_historic = (!isset($data_geocoding->address->historic) ? null : $data_geocoding->address->historic);
$geo_highway = (!isset($data_geocoding->address->highway) ? null : $data_geocoding->address->highway);
$geo_road = (!isset($data_geocoding->address->road) ? null : $data_geocoding->address->road);
$geo_neighbourhood = (!isset($data_geocoding->address->neighbourhood) ? null : $data_geocoding->address->neighbourhood);
$geo_suburb = (!isset($data_geocoding->address->suburb) ? null : $data_geocoding->address->suburb);
$geo_hamlet = (!isset($data_geocoding->address->hamlet) ? null : $data_geocoding->address->hamlet);
$geo_town = (!isset($data_geocoding->address->town) ? null : $data_geocoding->address->town);
$geo_village = (!isset($data_geocoding->address->village) ? null : $data_geocoding->address->village);
$geo_city = (!isset($data_geocoding->address->city) ? null : $data_geocoding->address->city);
$geo_state = (!isset($data_geocoding->address->state) ? null : $data_geocoding->address->state);
$geo_state_district = (!isset($data_geocoding->address->state_district) ? null : $data_geocoding->address->state_district);
$geo_municipality = (!isset($data_geocoding->address->municipality) ? null : $data_geocoding->address->municipality);
$geo_county = (!isset($data_geocoding->address->county) ? null : $data_geocoding->address->county);
$geo_postcode = (!isset($data_geocoding->address->postcode) ? null : str_replace(' ', '', $data_geocoding->address->postcode));
$geo_country = (!isset($data_geocoding->address->country) ? null : $data_geocoding->address->country);
$geo_country_code = (!isset($data_geocoding->address->country_code) ? null : $data_geocoding->address->country_code);
$geo_full = (!isset($data_geocoding->display_name) ? null : $data_geocoding->display_name);
$weather_dt_data = (!isset($data_weather->current->dt) ? null : $data_weather->current->dt);
$weather_id = (!isset($data_weather->current->weather[0]->id) ? null : $data_weather->current->weather[0]->id);
$weather_ic = (!isset($data_weather->current->weather[0]->icon) ? null : $data_weather->current->weather[0]->icon);
$weather_wm = (!isset($data_weather->current->weather[0]->main) ? null : $data_weather->current->weather[0]->main);
$weather_wd = (!isset($data_weather->current->weather[0]->description) ? null : $data_weather->current->weather[0]->description);
$weather_r_mm = (!isset($data_weather->current->rain) ? null : $data_weather->current->rain->{'1h'});
$weather_r_cm = ($weather_r_mm == null ? null : ($weather_r_mm / 10));
$weather_r_in = ($weather_r_mm == null ? null : ($weather_r_mm / 25.4));
$weather_s_mm = (!isset($data_weather->current->snow) ? null : $data_weather->current->snow->{'1h'});
$weather_s_cm = ($weather_s_mm == null ? null : ($weather_s_mm / 10));
$weather_s_in = ($weather_s_mm == null ? null : ($weather_s_mm / 25.4));
$weather_t_c = (!isset($data_weather->current->temp) ? null : $data_weather->current->temp);
$weather_t_f = ($weather_t_c == null ? null : ($weather_t_c * 9/5 + 32));
$weather_t_k = ($weather_t_c == null ? null : ($weather_t_c + 273.15));
$weather_tfl_c = (!isset($data_weather->current->feels_like) ? null : $data_weather->current->feels_like);
$weather_tfl_f = (!isset($data_weather->current->feels_like) ? null : ($weather_tfl_c * 9/5 + 32));
$weather_tfl_k = ($weather_tfl_c == null ? null : ($weather_tfl_c + 273.15));
$weather_tdp_c = (!isset($data_weather->current->dew_point) ? null : $data_weather->current->dew_point);
$weather_tdp_f = (!isset($data_weather->current->dew_point) ? null : ($weather_tdp_c * 9/5 + 32));
$weather_tdp_k = ($weather_tdp_c == null ? null : ($weather_tdp_c + 273.15));
$weather_ws_ms = (!isset($data_weather->current->wind_speed) ? null : $data_weather->current->wind_speed);
$weather_ws_g_ms = (!isset($data_weather->current->wind_gust) ? null : $data_weather->current->wind_gust);
$weather_ws_kmh = ($weather_ws_ms == null ? null : ($weather_ws_ms * 3.6));
$weather_ws_g_kmh = ($weather_ws_g_ms == null ? null : ($weather_ws_g_ms * 3.6));
$weather_ws_mph = ($weather_ws_ms == null ? null : ($weather_ws_ms / 0.44704));
$weather_ws_g_mph = ($weather_ws_g_ms == null ? null : ($weather_ws_g_ms / 0.44704));
$weather_wd_deg = (!isset($data_weather->current->wind_deg) ? null : $data_weather->current->wind_deg);
$weather_c = (!isset($data_weather->current->clouds) ? null : $data_weather->current->clouds);
$weather_uvi = (!isset($data_weather->current->uvi) ? null : $data_weather->current->uvi);
$weather_pr = (!isset($data_weather->current->pressure) ? null : $data_weather->current->pressure);
$weather_pr_inhg = (!isset($data_weather->current->pressure) ? null : ($weather_pr * 0.02953));
$weather_h = (!isset($data_weather->current->humidity) ? null : $data_weather->current->humidity);
$weather_h_h20perkgofair = ($weather_h == null ? null : calc_h2o_perkg_air($weather_h, $weather_t_c));
$weather_v_m = (!isset($data_weather->current->visibility) ? null : $data_weather->current->visibility);
$weather_v_km = (!isset($data_weather->current->visibility) ? null : ($weather_v_m * 0.001));
$weather_v_mi = (!isset($data_weather->current->visibility) ? null : ($weather_v_m * 0.00062137119));
$airpollution_aqi = (!isset($data_airpollution->list[0]->main->aqi) ? null : $data_airpollution->list[0]->main->aqi);
$airpollution_co = (!isset($data_airpollution->list[0]->components->co) ? null : $data_airpollution->list[0]->components->co);
$airpollution_no = (!isset($data_airpollution->list[0]->components->no) ? null : $data_airpollution->list[0]->components->no);
$airpollution_no2 = (!isset($data_airpollution->list[0]->components->no2) ? null : $data_airpollution->list[0]->components->no2);
$airpollution_o3 = (!isset($data_airpollution->list[0]->components->o3) ? null : $data_airpollution->list[0]->components->o3);
$airpollution_so2 = (!isset($data_airpollution->list[0]->components->so2) ? null : $data_airpollution->list[0]->components->so2);
$airpollution_pm2_5 = (!isset($data_airpollution->list[0]->components->pm2_5) ? null : $data_airpollution->list[0]->components->pm2_5);
$airpollution_pm10 = (!isset($data_airpollution->list[0]->components->pm10) ? null : $data_airpollution->list[0]->components->pm10);
$airpollution_nh3 = (!isset($data_airpollution->list[0]->components->nh3) ? null : $data_airpollution->list[0]->components->nh3);
if(!in_array('pollen', $arr_excluded)) {
/* $pollen_get = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.ambeedata.com/latest/pollen/by-lat-lng?lat='.$latitude.'&lng='.$longitude,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Content-type: application/json",
"x-api-key: [your-api-key]"
],
]);
$pollen_res = curl_exec($pollen_get);
$pollen_err = curl_error($pollen_get);
curl_close($pollen_get); */
$pollen = ((!isset($pollen_err) OR $pollen_err) ? null : json_decode($pollen_res, true));
$pollen_updatedat = ($pollen == null ? null : $pollen['data'][0]['updatedAt']);
$pollen_risk_grass = ($pollen == null ? null : $pollen['data'][0]['Risk']['grass_pollen']);
$pollen_risk_tree = ($pollen == null ? null : $pollen['data'][0]['Risk']['tree_pollen']);
$pollen_risk_weed = ($pollen == null ? null : $pollen['data'][0]['Risk']['weed_pollen']);
/* $pollen_count_grass = $pollen['data'][0]['Count']['grass_pollen'];
$pollen_count_tree = $pollen['data'][0]['Count']['tree_pollen'];
$pollen_count_weed = $pollen['data'][0]['Count']['weed_pollen']; */
$pollen_species_tree_alder = ($pollen == null ? null : $pollen['data'][0]['Species']['Tree']['Alder']);
$pollen_species_tree_birch = ($pollen == null ? null : $pollen['data'][0]['Species']['Tree']['Birch']);
$pollen_species_tree_cypress = ($pollen == null ? null : $pollen['data'][0]['Species']['Tree']['Cypress']);
$pollen_species_tree_elm = ($pollen == null ? null : $pollen['data'][0]['Species']['Tree']['Elm']);
$pollen_species_tree_hazel = ($pollen == null ? null : $pollen['data'][0]['Species']['Tree']['Hazel']);
$pollen_species_tree_oak = ($pollen == null ? null : $pollen['data'][0]['Species']['Tree']['Oak']);
$pollen_species_tree_pine = ($pollen == null ? null : $pollen['data'][0]['Species']['Tree']['Pine']);
$pollen_species_tree_plane = ($pollen == null ? null : $pollen['data'][0]['Species']['Tree']['Plane']);
$pollen_species_tree_poplar_cottonwood = ($pollen == null ? null : $pollen['data'][0]['Species']['Tree']['Poplar / Cottonwood']);
$pollen_species_weed_chenapod = ($pollen == null ? null : $pollen['data'][0]['Species']['Weed']['Chenopod']);
$pollen_species_weed_mugwort = ($pollen == null ? null : $pollen['data'][0]['Species']['Weed']['Mugwort']);
$pollen_species_weed_nettle = ($pollen == null ? null : $pollen['data'][0]['Species']['Weed']['Nettle']);
$pollen_species_weed_ragweed = ($pollen == null ? null : $pollen['data'][0]['Species']['Weed']['Ragweed']);
$pollen_species_grass_poaceae = ($pollen == null ? null : $pollen['data'][0]['Species']['Grass']['Grass / Poaceae']);
}
if(!isset($data_weather)) {
$arr = [
"id" => 200,
"message" => "Can't get data from OpenWeatherMap.",
"possible_solution" => "Check if the API key are correct and try again."
];
} else {
/*
PLACE
*/
if(!in_array('place', $arr_excluded)) {
$arr_place = [
'historic' => $geo_historic,
'highway' => $geo_highway,
'road' => $geo_road,
'neighbourhood' => $geo_neighbourhood,
'suburb' => $geo_suburb,
'hamlet' => $geo_hamlet,
'village' => $geo_village,
'town' => $geo_town,
'city' => $geo_city,
'state' => $geo_state,
'state_district' => $geo_state_district,
'municipality' => $geo_municipality,
'county' => $geo_county,
'post_code' => $geo_postcode,
'country' => $geo_country,
'country_code' => $geo_country_code,
'full' => $geo_full,
'link' => 'https://openstreetmap.org/#map=15/'.$latitude.'/'.$longitude
];
}
/*
WEATHER (current)
*/
$arr_wea_fetched = [
'time' => [
'twentyfourhours' => date_($weather_dt_data, 'time-24h-s'),
'twelvehours' => date_($weather_dt_data, 'time-12h-s')
]
];
$arr_wea_current = [
'weather' => [
'id' => $weather_id,
'id_icon' => $weather_ic,
'icon' => weathericon($weather_ic . $weather_id),
'main' => $weather_wm,
'description' => $weather_wd,
'rain' => [
'mm' => (float)format_number($weather_r_mm, 2, '.'),
'cm' => (float)format_number($weather_r_cm, 2, '.'),
'in' => (float)format_number($weather_r_in, 2, '.')
],
'snow' => [
'mm' => (float)format_number($weather_s_mm, 2, '.'),
'cm' => (float)format_number($weather_s_cm, 2, '.'),
'in' => (float)format_number($weather_s_in, 2, '.')
],
'globe_map' => 'https://api.met.no/weatherapi/geosatellite/1.4/?area=global&type=infrared'
],
'temp' => [
'celcius' => (float)format_number($weather_t_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_t_f, 1, '.'),
'kelvin' => (float)format_number($weather_t_k, 1, '.')
],
'temp_feels_like' => [
'celcius' => (float)format_number($weather_tfl_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_tfl_f, 1, '.'),
'kelvin' => (float)format_number($weather_tfl_k, 1, '.')
],
'temp_dew_point' => [
'celcius' => (float)format_number($weather_tdp_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_tdp_f, 1, '.'),
'kelvin' => (float)format_number($weather_tdp_k, 1, '.')
],
'wind' => [
'descriptions' => [
'land' => wind_descriptions((float)format_number($weather_ws_ms, 1, '.'), 'land'),
'sea' => wind_descriptions((float)format_number($weather_ws_ms, 1, '.'), 'sea')
],
'speed' => [
'ms' => [
'wind' => (float)format_number($weather_ws_ms, 1, '.'),
'gust' => ($weather_ws_g_ms == null ? null : (float)format_number($weather_ws_g_ms, 1, '.'))
],
'kmh' => [
'wind' => (float)format_number($weather_ws_kmh, 1, '.'),
'gust' => ($weather_ws_g_kmh == null ? null : (float)format_number($weather_ws_g_kmh, 1, '.'))
],
'mph' => [
'wind' => (float)format_number($weather_ws_mph, 1, '.'),
'gust' => ($weather_ws_g_mph == null ? null : (float)format_number($weather_ws_g_mph, 1, '.'))
]
],
'direction' => [
'degrees' => (int)$weather_wd_deg,
'compass' => degrees_to_compass((int)$weather_wd_deg)
]
],
'clouds' => (int)$weather_c,
'humidity' => (int)$weather_h,
'h2o_per_kg_of_air' => [
'value' => format_number((float)$weather_h_h20perkgofair, 2, '.', true),
'type' => 'g'
],
'pressure' => [
'hpa' => (int)$weather_pr,
'inhg' => (float)format_number($weather_pr_inhg, 4, '.')
],
'uv_index' => [
"grade" => (int)$weather_uvi,
"risk" => uvindex_grade((int)$weather_uvi, 'risk'),
"colour" => uvindex_grade((int)$weather_uvi, 'colour')
],
'visibility' => [
'm' => (int)$weather_v_m,
'km' => (float)format_number($weather_v_km, 2, '.'),
'mi' => (float)format_number($weather_v_mi, 2, '.')
]
];
$arr_wea_minutely = [];
if(isset($data_weather->minutely)) {
foreach($data_weather->minutely AS $minutely) {
$weather_dt_data = (!isset($minutely->dt) ? null : $minutely->dt);
$weather_m_precip = (!isset($minutely->precipitation) ? null : $minutely->precipitation);
$arr_wea_minutely[] = [
'timestamp' => $weather_dt_data,
'date' => date_($weather_dt_data, 'date'),
'time' => [
'twentyfourhours' => date_($weather_dt_data, 'time-24h'),
'twelvehours' => date_($weather_dt_data, 'time-12h')
],
'precipitation' => $weather_m_precip
];
}
}
$arr_wea_hourly = [];
foreach($data_weather->hourly AS $hourly) {
$weather_dt_data = (!isset($hourly->dt) ? null : $hourly->dt);
$weather_h_id = (!isset($hourly->weather[0]->id) ? null : $hourly->weather[0]->id);
$weather_h_ic = (!isset($hourly->weather[0]->icon) ? null : $hourly->weather[0]->icon);
$weather_h_wm = (!isset($hourly->weather[0]->main) ? null : $hourly->weather[0]->main);
$weather_h_wd = (!isset($hourly->weather[0]->description) ? null : $hourly->weather[0]->description);
$weather_h_p = (!isset($hourly->pop) ? null : ($hourly->pop * 100));
$weather_h_r_mm = (!isset($hourly->rain->{'1h'}) ? null : $hourly->rain->{'1h'});
$weather_h_r_cm = ($weather_h_r_mm == null ? null : ($weather_h_r_mm / 10));
$weather_h_r_in = ($weather_h_r_mm == null ? null : ($weather_h_r_mm / 25.4));
$weather_h_s_mm = (!isset($hourly->snow->{'1h'}) ? null : $hourly->snow->{'1h'});
$weather_h_s_cm = ($weather_h_s_mm == null ? null : ($weather_h_s_mm / 10));
$weather_h_s_in = ($weather_h_s_mm == null ? null : ($weather_h_s_mm / 25.4));
$weather_h_t_c = (!isset($hourly->temp) ? null : $hourly->temp);
$weather_h_t_f = (!isset($hourly->temp) ? null : ($weather_h_t_c * 9/5 + 32));
$weather_h_t_k = (!isset($hourly->temp) ? null : ($weather_h_t_c + 273.15));
$weather_h_tfl_c = (!isset($hourly->feels_like) ? null : $hourly->feels_like);
$weather_h_tfl_f = (!isset($hourly->feels_like) ? null : ($weather_h_tfl_c * 9/5 + 32));
$weather_h_tfl_k = (!isset($hourly->feels_like) ? null : ($weather_h_tfl_c + 273.15));
$weather_h_tdp_c = (!isset($hourly->dew_point) ? null : $hourly->dew_point);
$weather_h_tdp_f = (!isset($hourly->dew_point) ? null : ($weather_h_tdp_c * 9/5 + 32));
$weather_h_tdp_k = (!isset($hourly->feels_like) ? null : ($weather_h_tdp_c + 273.15));
$weather_h_ws_ms = (!isset($hourly->wind_speed) ? null : $hourly->wind_speed);
$weather_h_ws_g_ms = (!isset($hourly->wind_gust) ? null : $hourly->wind_gust);
$weather_h_ws_kmh = ($weather_h_ws_ms == null ? null : ($weather_h_ws_ms * 3.6));
$weather_h_ws_g_kmh = ($weather_h_ws_g_ms == null ? null : ($weather_h_ws_g_ms * 3.6));
$weather_h_ws_mph = ($weather_h_ws_ms == null ? null : ($weather_h_ws_ms / 0.44704));
$weather_h_ws_g_mph = ($weather_h_ws_g_ms == null ? null : ($weather_h_ws_g_ms / 0.44704));
$weather_h_wd_deg = (!isset($hourly->wind_deg) ? null : $hourly->wind_deg);
$weather_h_c = (!isset($hourly->clouds) ? null : $hourly->clouds);
$weather_h_uvi = (!isset($hourly->uvi) ? null : $hourly->uvi);
$weather_h_pr = (!isset($hourly->pressure) ? null : $hourly->pressure);
$weather_h_pr_inhg = (!isset($hourly->pressure) ? null : ($weather_h_pr * 0.02953));
$weather_h_h = (!isset($hourly->humidity) ? null : $hourly->humidity);
$weather_h_h20perkgofair = ($weather_h_h == null ? null : calc_h2o_perkg_air($weather_h_h, $weather_h_t_c));
$weather_h_v_m = (!isset($hourly->visibility) ? null : $hourly->visibility);
$weather_h_v_km = (!isset($hourly->visibility) ? null : ($weather_h_v_m * 0.001));
$weather_h_v_mi = (!isset($hourly->visibility) ? null : ($weather_h_v_m * 0.00062137119));;
$arr_wea_hourly[] = [
'timestamp' => $weather_dt_data,
'date' => date_($weather_dt_data, 'date'),
'time' => [
'twentyfourhours' => date_($weather_dt_data, 'time-24h'),
'twelvehours' => date_($weather_dt_data, 'time-12h')
],
'weather' => [
'id' => $weather_h_id,
'id_icon' => $weather_h_ic,
'icon' => weathericon($weather_h_ic . $weather_h_id),
'main' => $weather_h_wm,
'description' => $weather_h_wd,
'prob_precip' => (float)format_number($weather_h_p, 0),
'rain' => [
'mm' => (float)format_number($weather_h_r_mm, 2, '.'),
'cm' => (float)format_number($weather_h_r_cm, 2, '.'),
'in' => (float)format_number($weather_h_r_in, 2, '.')
],
'snow' => [
'mm' => (float)format_number($weather_h_s_mm, 2, '.'),
'cm' => (float)format_number($weather_h_s_cm, 2, '.'),
'in' => (float)format_number($weather_h_s_in, 2, '.')
]
],
'temp' => [
'celcius' => (float)format_number($weather_h_t_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_h_t_f, 1, '.'),
'kelvin' => (float)format_number($weather_h_t_k, 1, '.')
],
'temp_feels_like' => [
'celcius' => (float)format_number($weather_h_tfl_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_h_tfl_f, 1, '.'),
'kelvin' => (float)format_number($weather_h_tfl_k, 1, '.')
],
'temp_dew_point' => [
'celcius' => (float)format_number($weather_h_tdp_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_h_tdp_f, 1, '.'),
'kelvin' => (float)format_number($weather_h_tdp_k, 1, '.')
],
'wind' => [
'descriptions' => [
'land' => wind_descriptions((float)format_number($weather_h_ws_ms, 1, '.'), 'land'),
'sea' => wind_descriptions((float)format_number($weather_h_ws_ms, 1, '.'), 'sea')
],
'speed' => [
'ms' => [
'wind' => (float)format_number($weather_h_ws_ms, 1, '.'),
'gust' => ($weather_h_ws_g_ms == null ? null : (float)format_number($weather_h_ws_g_ms, 1, '.'))
],
'kmh' => [
'wind' => (float)format_number($weather_h_ws_kmh, 1, '.'),
'gust' => ($weather_h_ws_g_kmh == null ? null : (float)format_number($weather_h_ws_g_kmh, 1, '.'))
],
'mph' => [
'wind' => (float)format_number($weather_h_ws_mph, 1, '.'),
'gust' => ($weather_h_ws_g_mph == null ? null : (float)format_number($weather_h_ws_g_mph, 1, '.'))
]
],
'direction' => [
'degrees' => (int)$weather_h_wd_deg,
'compass' => degrees_to_compass((int)$weather_h_wd_deg)
]
],
'clouds' => (int)$weather_h_c,
'humidity' => (int)$weather_h_h,
'h2o_per_kg_of_air' => [
'value' => format_number((float)$weather_h_h20perkgofair, 2, '.', true),
'type' => 'g'
],
'pressure' => [
'hpa' => (int)$weather_h_pr,
'inhg' => (float)format_number($weather_h_pr_inhg, 4, '.')
],
'uv_index' => [
"grade" => (int)$weather_h_uvi,
"risk" => uvindex_grade((int)$weather_h_uvi, 'risk'),
"colour" => uvindex_grade((int)$weather_h_uvi, 'colour')
],
'visibility' => [
'km' => (float)format_number($weather_h_v_km, 2, '.'),
'mi' => (float)format_number($weather_h_v_mi, 2, '.')
]
];
}
$arr_wea_daily = [];
foreach($data_weather->daily AS $daily) {
$weather_dt_data = (!isset($daily->dt) ? null : $daily->dt);
$weather_d_id = (!isset($daily->weather[0]->id) ? null : $daily->weather[0]->id);
$weather_d_ic = (!isset($daily->weather[0]->icon) ? null : $daily->weather[0]->icon);
$weather_d_wm = (!isset($daily->weather[0]->main) ? null : $daily->weather[0]->main);
$weather_d_wd = (!isset($daily->weather[0]->description) ? null : $daily->weather[0]->description);
$weather_d_p = (!isset($daily->pop) ? null : ($daily->pop * 100));
$weather_d_r_mm = (!isset($daily->rain) ? null : $daily->rain);
$weather_d_r_cm = ($weather_d_r_mm == null ? null : ($weather_d_r_mm / 10));
$weather_d_r_in = ($weather_d_r_mm == null ? null : ($weather_d_r_mm / 25.4));
$weather_d_s_mm = (!isset($daily->snow) ? null : $daily->snow);
$weather_d_s_cm = ($weather_d_s_mm == null ? null : ($weather_d_s_mm / 10));
$weather_d_s_in = ($weather_d_s_mm == null ? null : ($weather_d_s_mm / 25.4));
$weather_d_tmi_c = (!isset($daily->temp->min) ? null : $daily->temp->min);
$weather_d_tmi_f = ($weather_d_tmi_c == null ? null : ($weather_d_tmi_c * 9/5 + 32));
$weather_d_tmi_k = ($weather_d_tmi_c == null ? null : ($weather_d_tmi_c + 273.15));
$weather_d_tma_c = (!isset($daily->temp->max) ? null : $daily->temp->max);
$weather_d_tma_f = ($weather_d_tma_c == null ? null : ($weather_d_tma_c * 9/5 + 32));
$weather_d_tma_k = ($weather_d_tma_c == null ? null : ($weather_d_tma_c + 273.15));
$weather_d_tm_c = (!isset($daily->temp->morn) ? null : $daily->temp->morn);
$weather_d_tm_f = ($weather_d_tm_c == null ? null : ($weather_d_tm_c * 9/5 + 32));
$weather_d_tm_k = ($weather_d_tm_c == null ? null : ($weather_d_tm_c + 273.15));
$weather_d_td_c = (!isset($daily->temp->day) ? null : $daily->temp->day);
$weather_d_td_f = ($weather_d_td_c == null ? null : ($weather_d_td_c * 9/5 + 32));
$weather_d_td_k = ($weather_d_td_c == null ? null : ($weather_d_td_c + 273.15));
$weather_d_te_c = (!isset($daily->temp->eve) ? null : $daily->temp->eve);
$weather_d_te_f = ($weather_d_te_c == null ? null : ($weather_d_te_c * 9/5 + 32));
$weather_d_te_k = ($weather_d_te_c == null ? null : ($weather_d_te_c + 273.15));
$weather_d_tn_c = (!isset($daily->temp->night) ? null : $daily->temp->night);
$weather_d_tn_f = ($weather_d_tn_c == null ? null : ($weather_d_tn_c * 9/5 + 32));
$weather_d_tn_k = ($weather_d_tn_c == null ? null : ($weather_d_tn_c + 273.15));
$weather_d_tflm_c = (!isset($daily->feels_like->morn) ? null : $daily->feels_like->morn);
$weather_d_tflm_f = ($weather_d_tflm_c == null ? null : ($weather_d_tflm_c * 9/5 + 32));
$weather_d_tflm_k = ($weather_d_tflm_c == null ? null : ($weather_d_tflm_c + 273.15));
$weather_d_tfld_c = (!isset($daily->feels_like->day) ? null : $daily->feels_like->day);
$weather_d_tfld_f = ($weather_d_tfld_c == null ? null : ($weather_d_tfld_c * 9/5 + 32));
$weather_d_tfld_k = ($weather_d_tfld_c == null ? null : ($weather_d_tfld_c + 273.15));
$weather_d_tfle_c = (!isset($daily->feels_like->eve) ? null : $daily->feels_like->eve);
$weather_d_tfle_f = ($weather_d_tfle_c == null ? null : ($weather_d_tfle_c * 9/5 + 32));
$weather_d_tfle_k = ($weather_d_tfle_c == null ? null : ($weather_d_tfle_c + 273.15));
$weather_d_tfln_c = (!isset($daily->feels_like->night) ? null : $daily->feels_like->night);
$weather_d_tfln_f = ($weather_d_tfln_c == null ? null : ($weather_d_tfln_c * 9/5 + 32));
$weather_d_tfln_k = ($weather_d_tfln_c == null ? null : ($weather_d_tfln_c + 273.15));
$weather_d_tdp_c = (!isset($daily->dew_point) ? null : $daily->dew_point);
$weather_d_tdp_f = ($weather_d_tdp_c == null ? null : ($weather_d_tdp_c * 9/5 + 32));
$weather_d_tdp_k = ($weather_d_tdp_c == null ? null : ($weather_d_tdp_c + 273.15));
$weather_d_ws_ms = (!isset($daily->wind_speed) ? null : $daily->wind_speed);
$weather_d_ws_g_ms = (!isset($daily->wind_gust) ? null : $daily->wind_gust);
$weather_d_ws_kmh = ($weather_d_ws_ms == null ? null : ($weather_d_ws_ms * 3.6));
$weather_d_ws_g_kmh = ($weather_d_ws_g_ms == null ? null : ($weather_d_ws_g_ms * 3.6));
$weather_d_ws_mph = ($weather_d_ws_ms == null ? null : ($weather_d_ws_ms / 0.44704));
$weather_d_ws_g_mph = ($weather_d_ws_g_ms == null ? null : ($weather_d_ws_g_ms / 0.44704));
$weather_d_wd_deg = (!isset($daily->wind_deg) ? null : $daily->wind_deg);
$weather_d_c = (!isset($daily->clouds) ? null : $daily->clouds);
$weather_d_uvi = (!isset($daily->uvi) ? null : $daily->uvi);
$weather_d_pr = (!isset($daily->pressure) ? null : $daily->pressure);
$weather_d_pr_inhg = (!isset($daily->pressure) ? null : ($weather_d_pr * 0.02953));
$weather_d_h = (!isset($daily->humidity) ? null : $daily->humidity);
$weather_d_h20perkgofair = ($weather_d_h == null ? null : calc_h2o_perkg_air($weather_d_h, $weather_d_td_c));
$arr_wea_daily[] = [
'timestamp' => $weather_dt_data,
'date' => [
'date' => date_($weather_dt_data, 'date'),
'details' => [
'year' => (int)date('Y', $weather_dt_data),
'month' => [
'number' => (int)date('n', $weather_dt_data),
'name' => [
'full' => date('F', $weather_dt_data),
'short' => date('M', $weather_dt_data)
]
],
'day' => [
'number' => (int)date('j', $weather_dt_data),
'suffix' => date('S', $weather_dt_data),
'weekday' => [
'full' => date('l', $weather_dt_data),
'short' => date('D', $weather_dt_data)
]
],
'week' => (int)date('W', $weather_dt_data)
]
],
'time' => [
'twentyfourhours' => date_($weather_dt_data, 'time-24h'),
'twelvehours' => date_($weather_dt_data, 'time-12h')
],
'weather' => [
'id' => $weather_d_id,
'id_icon' => $weather_d_ic,
'icon' => weathericon($weather_d_ic . $weather_d_id),
'main' => $weather_d_wm,
'description' => $weather_d_wd,
'prob_precip' => (float)format_number($weather_d_p, 0),
'rain' => [
'mm' => (float)format_number($weather_d_r_mm, 2, '.'),
'cm' => (float)format_number($weather_d_r_cm, 2, '.'),
'in' => (float)format_number($weather_d_r_in, 2, '.')
],
'snow' => [
'mm' => (float)format_number($weather_d_s_mm, 2, '.'),
'cm' => (float)format_number($weather_d_s_cm, 2, '.'),
'in' => (float)format_number($weather_d_s_in, 2, '.')
]
],
'temps' => [
'min' => [
'celcius' => (float)format_number($weather_d_tmi_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_d_tmi_f, 1, '.'),
'kelvin' => (float)format_number($weather_d_tmi_k, 1, '.')
],
'max' => [
'celcius' => (float)format_number($weather_d_tma_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_d_tma_f, 1, '.'),
'kelvin' => (float)format_number($weather_d_tma_k, 1, '.')
],
'morning' => [
'celcius' => (float)format_number($weather_d_tm_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_d_tm_f, 1, '.'),
'kelvin' => (float)format_number($weather_d_tm_k, 1, '.')
],
'day' => [
'celcius' => (float)format_number($weather_d_td_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_d_td_f, 1, '.'),
'kelvin' => (float)format_number($weather_d_td_k, 1, '.')
],
'evening' => [
'celcius' => (float)format_number($weather_d_te_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_d_te_f, 1, '.'),
'kelvin' => (float)format_number($weather_d_te_k, 1, '.')
],
'night' => [
'celcius' => (float)format_number($weather_d_tn_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_d_tn_f, 1, '.'),
'kelvin' => (float)format_number($weather_d_tn_k, 1, '.')
]
],
'temp_feels_like' => [
'morning' => [
'celcius' => (float)format_number($weather_d_tflm_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_d_tflm_f, 1, '.'),
'kelvin' => (float)format_number($weather_d_tflm_k, 1, '.')
],
'day' => [
'celcius' => (float)format_number($weather_d_tfld_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_d_tfld_f, 1, '.'),
'kelvin' => (float)format_number($weather_d_tfld_k, 1, '.')
],
'evening' => [
'celcius' => (float)format_number($weather_d_tfle_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_d_tfle_f, 1, '.'),
'kelvin' => (float)format_number($weather_d_tfle_k, 1, '.')
],
'night' => [
'celcius' => (float)format_number($weather_d_tfln_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_d_tfln_f, 1, '.'),
'kelvin' => (float)format_number($weather_d_tfln_k, 1, '.')
]
],
'temp_dew_point' => [
'celcius' => (float)format_number($weather_d_tdp_c, 1, '.'),
'fahrenheit' => (float)format_number($weather_d_tdp_f, 1, '.'),
'kelvin' => (float)format_number($weather_d_tdp_k, 1, '.')
],
'wind' => [
'descriptions' => [
'land' => wind_descriptions((float)format_number($weather_d_ws_ms, 1, '.'), 'land'),
'sea' => wind_descriptions((float)format_number($weather_d_ws_ms, 1, '.'), 'sea')
],
'speed' => [
'ms' => [
'wind' => (float)format_number($weather_d_ws_ms, 1, '.'),
'gust' => ($weather_d_ws_g_ms == null ? null : (float)format_number($weather_d_ws_g_ms, 1, '.'))
],
'kmh' => [
'wind' => (float)format_number($weather_d_ws_kmh, 1, '.'),
'gust' => ($weather_d_ws_g_kmh == null ? null : (float)format_number($weather_d_ws_g_kmh, 1, '.'))
],
'mph' => [
'wind' => (float)format_number($weather_d_ws_mph, 1, '.'),
'gust' => ($weather_d_ws_g_mph == null ? null : (float)format_number($weather_d_ws_g_mph, 1, '.'))
]
],
'direction' => [
'degrees' => (int)$weather_d_wd_deg,
'compass' => degrees_to_compass((int)$weather_d_wd_deg)
]
],
'clouds' => (int)$weather_d_c,
'humidity' => (int)$weather_d_h,
'h2o_per_kg_of_air' => [
'value' => format_number((float)$weather_d_h20perkgofair, 2, '.', true),
'type' => 'g'
],
'pressure' => [
'hpa' => (int)$weather_d_pr,
'inhg' => (float)format_number($weather_d_pr_inhg, 4, '.')
],
'uv_index' => [
"grade" => (int)$weather_d_uvi,
"risk" => uvindex_grade((int)$weather_d_uvi, 'risk'),
"colour" => uvindex_grade((int)$weather_d_uvi, 'colour')
]
];
}
$arr_alerts = [];
if(isset($data_weather->alerts)) {
foreach($data_weather->alerts AS $alert) {
$weather_a_start = (!isset($alert->start) ? null : $alert->start);
$weather_a_end = (!isset($alert->end) ? null : $alert->end);
$weather_a_event = (!isset($alert->event) ? null : $alert->event);
$weather_a_sender = (!isset($alert->sender_name) ? null : $alert->sender_name);
$weather_a_description = (!isset($alert->description) ? null : $alert->description);
$arr_tags = [];
foreach($alert->tags AS $tag) {
$arr_tags[] = $tag;
}
$arr_alerts[] = [
'timestamps' => [
'start' => $weather_a_start,
'end' => $weather_a_end
],
'event' => $weather_a_event,
'sender' => $weather_a_sender,
'description' => $weather_a_description,
'tags' => $arr_tags
];
}
}
if(!in_array('pollen', $arr_excluded)) {
$arr_pollen = [
'message' => ($pollen == null ? 'Couldn\'t get pollen data.' : null),
'updated' => [
'timestamp' => ($pollen == null ? null : strtotime($pollen_updatedat)),
'date' => ($pollen == null ? null : date_(strtotime($pollen_updatedat), 'date')),
'time' => [
'twentyfourhours' => ($pollen == null ? null : date_(strtotime($pollen_updatedat), 'time-24h-s')),
'twelvehours' => ($pollen == null ? null : date_(strtotime($pollen_updatedat), 'time-12h-s'))
]
],
'grass' => [
'risk' => $pollen_risk_grass,
'count' => $pollen_species_grass_poaceae,
'species' => [
'grass_poaceae' => $pollen_species_grass_poaceae
]
],
'tree' => [
'risk' => $pollen_risk_tree,
'count' => ($pollen == null ? null : ($pollen_species_tree_alder + $pollen_species_tree_birch + $pollen_species_tree_cypress + $pollen_species_tree_elm + $pollen_species_tree_hazel + $pollen_species_tree_oak + $pollen_species_tree_pine + $pollen_species_tree_plane + $pollen_species_tree_poplar_cottonwood)),
'species' => [
'alder' => $pollen_species_tree_alder,
'birch' => $pollen_species_tree_birch,
'cypress' => $pollen_species_tree_cypress,
'elm' => $pollen_species_tree_elm,
'hazel' => $pollen_species_tree_hazel,
'oak' => $pollen_species_tree_oak,
'pine' => $pollen_species_tree_pine,
'plane' => $pollen_species_tree_plane,
'poplar_cottonwood' => $pollen_species_tree_poplar_cottonwood
]
],
'weed' => [
'risk' => $pollen_risk_weed,
'count' => ($pollen == null ? null : ($pollen_species_weed_chenapod + $pollen_species_weed_mugwort + $pollen_species_weed_nettle + $pollen_species_weed_ragweed)),
'species' => [
'chenapod' => $pollen_species_weed_chenapod,
'mugwort' => $pollen_species_weed_mugwort,
'nettle' => $pollen_species_weed_nettle,
'ragweed' => $pollen_species_weed_ragweed
]
]
];
}
if(!in_array('airp', $arr_excluded)) {
$arr_airpollution = [
'quality' => [
"grade" => air_pollution_quality((int)$airpollution_aqi, 'grade'),
"colour" => air_pollution_quality((int)$airpollution_aqi, 'colour')
],
'aqi' => (int)$airpollution_aqi,
'co' => (float)$airpollution_co,
'no' => (float)$airpollution_no,
'no2' => (float)$airpollution_no2,
'o3' => (float)$airpollution_o3,
'so2' => (float)$airpollution_so2,
'pm25' => (float)$airpollution_pm2_5,
'pm10' => (float)$airpollution_pm10,
'nh3' => (float)$airpollution_nh3
];
}
$arr_weather = [
'fetched' => $arr_wea_fetched,
'weather' => $arr_wea_current,
'pollen' => (in_array('pollen', $arr_excluded) ? null : $arr_pollen),
'air_pollution' => (in_array('airp', $arr_excluded) ? null : $arr_airpollution)
];
/*
ASTRONOMY
*/
if(!in_array('astronomy', $arr_excluded)) {
$daylight_rise = new DateTime(date_($astro_sun_rise, 'datetime'), new DateTimeZone($timezone));
$daylight_set = new DateTime(date_($astro_sun_set, 'datetime'), new DateTimeZone($timezone));
$daylight = $daylight_set->diff($daylight_rise);
$night_starts = new DateTime(date_($astro_sun_night, 'datetime'), new DateTimeZone($timezone));
$night_ends = new DateTime(date_($astro_sun_rise, 'datetime'), new DateTimeZone($timezone));
$night = $night_starts->diff($night_ends);
$arr_astro_sun = [
'altitude' => (float)format_number($astro_sun_altitude, 2, '.'),
'azimuth' => (float)format_number($astro_sun_azimuth, 2, '.'),
'is_above_horizon' => $astro_sun_isup,
'always_up' => (($daylight->h == 0 AND $daylight->i == 0 OR $astro_sun_rise == null) ? true : false),
'always_down' => (($night->h == 0 AND $night->i == 0 OR $astro_sun_night == null) ? true : false),
'durations' => [
'daylight' => [
'hours' => $daylight->h,
'minutes' => $daylight->i
],
'night' => [
'hours' => $night->h,
'minutes' => $night->i
]
],
'rise' => [
'time' => [
'twentyfourhours' => date_($astro_sun_rise, 'time-24h'),
'twelvehours' => date_($astro_sun_rise, 'time-12h')
]
],
'rise_end' => [
'time' => [
'twentyfourhours' => date_($astro_sun_riseend, 'time-24h'),
'twelvehours' => date_($astro_sun_riseend, 'time-12h')
]
],
'goldenhour_end' => [
'time' => [
'twentyfourhours' => date_($astro_sun_goldenhourend, 'time-24h'),
'twelvehours' => date_($astro_sun_goldenhourend, 'time-12h')
]
],
'solarnoon' => [
'date' => date_($astro_sun_solarnoon, 'date'),
'time' => [
'twentyfourhours' => date_($astro_sun_solarnoon, 'time-24h'),
'twelvehours' => date_($astro_sun_solarnoon, 'time-12h')
]
],
'goldenhour' => [
'time' => [
'twentyfourhours' => date_($astro_sun_goldenhour, 'time-24h'),
'twelvehours' => date_($astro_sun_goldenhour, 'time-12h')
]
],
'set_start' => [
'time' => [
'twentyfourhours' => date_($astro_sun_setstart, 'time-24h'),
'twelvehours' => date_($astro_sun_setstart, 'time-12h')
]
],
'set' => [
'time' => [
'twentyfourhours' => date_($astro_sun_set, 'time-24h'),
'twelvehours' => date_($astro_sun_set, 'time-12h')
]
],
'dusk' => [
'time' => [
'twentyfourhours' => date_($astro_sun_dusk, 'time-24h'),
'twelvehours' => date_($astro_sun_dusk, 'time-12h')
]
],
'nautical_dusk' => [
'time' => [
'twentyfourhours' => date_($astro_sun_nauticaldusk, 'time-24h'),
'twelvehours' => date_($astro_sun_nauticaldusk, 'time-12h')
]
],
'night' => [
'time' => [
'twentyfourhours' => date_($astro_sun_night, 'time-24h'),
'twelvehours' => date_($astro_sun_night, 'time-12h')
]
],
'nadir' => [
'time' => [
'twentyfourhours' => date_($astro_sun_nadir, 'time-24h'),
'twelvehours' => date_($astro_sun_nadir, 'time-12h')
]
],
'night_end' => [
'time' => [
'twentyfourhours' => date_($astro_sun_nightend, 'time-24h'),
'twelvehours' => date_($astro_sun_nightend, 'time-12h')
]
],
'nautical_dawn' => [
'time' => [
'twentyfourhours' => date_($astro_sun_nauticaldawn, 'time-24h'),
'twelvehours' => date_($astro_sun_nauticaldawn, 'time-12h')
]
],
'dawn' => [
'time' => [
'twentyfourhours' => date_($astro_sun_dawn, 'time-24h'),
'twelvehours' => date_($astro_sun_dawn, 'time-12h')
]
]
];
$arr_astro_moon = [
'phase' => [
'phase' => (float)format_number($astro_moon_phase, 5, '.'),
'name' => $astro_moon_phase_name
],
'altitude' => (float)format_number($astro_moon_altitude, 2, '.'),
'azimuth' => (float)format_number($astro_moon_azimuth, 2, '.'),
'fraction' => (float)format_number($astro_moon_fraction, 1, '.'),
'angle' => (float)format_number($astro_moon_angle, 1, '.'),
'age' => (float)format_number($astro_moon_age, 1, '.'),
'distances' => [
'earth' => (float)str_replace(' ', '', format_number($astro_moon_distance_earth, 2, '.')),
'sun' => (float)str_replace(' ', '', format_number($astro_moon_distance_sun, 2, '.'))
],
'diameters' => [
'moon' => (float)str_replace(' ', '', format_number($astro_moon_diameter_moon, 2, '.')),
'sun' => (float)str_replace(' ', '', format_number($astro_moon_diameter_sun, 2, '.'))
],
'rise' => [
'twentyfourhours' => date_($astro_moon_rise, 'time-24h'),
'twelvehours' => date_($astro_moon_rise, 'time-12h')
],
'set' => [
'twentyfourhours' => date_($astro_moon_set, 'time-24h'),
'twelvehours' => date_($astro_moon_set, 'time-12h')
],
'always_up' => ($astro_moon_alwaysup != null ? true : false),
'always_down' => ($astro_moon_alwaysdown != null ? true : false),
'schedules' => [
'first_quarter' => [
'timestamp' => $astro_moon_firstquarter,
'date' => date_($astro_moon_firstquarter, 'date'),
'time' => [
'twentyfourhours' => date_($astro_moon_firstquarter, 'time-24h'),
'twelvehours' => date_($astro_moon_firstquarter, 'time-12h')
],
'countdown' => [
'days' => timeleft($astro_moon_firstquarter, true),
'hours' => timeleft($astro_moon_firstquarter, false, true),
'minutes' => timeleft($astro_moon_firstquarter, false, false, true)
]
],
'next_first_quarter' => [
'timestamp' => $astro_moon_nextfirstquarter,
'date' => date_($astro_moon_nextfirstquarter, 'date'),
'time' => [
'twentyfourhours' => date_($astro_moon_nextfirstquarter, 'time-24h'),
'twelvehours' => date_($astro_moon_nextfirstquarter, 'time-12h')
],
'countdown' => [
'days' => timeleft($astro_moon_nextfirstquarter, true),
'hours' => timeleft($astro_moon_nextfirstquarter, false, true),
'minutes' => timeleft($astro_moon_nextfirstquarter, false, false, true)
]
],
'full' => [
'timestamp' => $astro_moon_full,
'date' => date_($astro_moon_full, 'date'),
'time' => [
'twentyfourhours' => date_($astro_moon_full, 'time-24h'),
'twelvehours' => date_($astro_moon_full, 'time-12h')
],
'countdown' => [
'days' => timeleft($astro_moon_full, true),
'hours' => timeleft($astro_moon_full, false, true),
'minutes' => timeleft($astro_moon_full, false, false, true)
]
],
'next_full' => [
'timestamp' => $astro_moon_nextfull,
'date' => date_($astro_moon_nextfull, 'date'),
'time' => [
'twentyfourhours' => date_($astro_moon_nextfull, 'time-24h'),
'twelvehours' => date_($astro_moon_nextfull, 'time-12h')
],
'countdown' => [
'days' => timeleft($astro_moon_nextfull, true),
'hours' => timeleft($astro_moon_nextfull, false, true),
'minutes' => timeleft($astro_moon_nextfull, false, false, true)
]
],
'last_quarter' => [
'timestamp' => $astro_moon_lastquarter,
'date' => date_($astro_moon_lastquarter, 'date'),
'time' => [
'twentyfourhours' => date_($astro_moon_lastquarter, 'time-24h'),
'twelvehours' => date_($astro_moon_lastquarter, 'time-12h')
],
'countdown' => [
'days' => timeleft($astro_moon_lastquarter, true),
'hours' => timeleft($astro_moon_lastquarter, false, true),
'minutes' => timeleft($astro_moon_lastquarter, false, false, true)
]
],
'next_last_quarter' => [
'timestamp' => $astro_moon_nextlastquarter,
'date' => date_($astro_moon_nextlastquarter, 'date'),
'time' => [
'twentyfourhours' => date_($astro_moon_nextlastquarter, 'time-24h'),
'twelvehours' => date_($astro_moon_nextlastquarter, 'time-12h')
],
'countdown' => [
'days' => timeleft($astro_moon_nextlastquarter, true),
'hours' => timeleft($astro_moon_nextlastquarter, false, true),
'minutes' => timeleft($astro_moon_nextlastquarter, false, false, true)
]
],
'new' => [
'timestamp' => $astro_moon_new,
'date' => date_($astro_moon_new, 'date'),
'time' => [
'twentyfourhours' => date_($astro_moon_new, 'time-24h'),
'twelvehours' => date_($astro_moon_new, 'time-12h')
],
'countdown' => [
'days' => timeleft($astro_moon_new, true),
'hours' => timeleft($astro_moon_new, false, true),
'minutes' => timeleft($astro_moon_new, false, false, true)
]
],
'next_new' => [
'timestamp' => $astro_moon_nextnew,
'date' => date_($astro_moon_nextnew, 'date'),
'time' => [
'twentyfourhours' => date_($astro_moon_nextnew, 'time-24h'),
'twelvehours' => date_($astro_moon_nextnew, 'time-12h')
],
'countdown' => [
'days' => timeleft($astro_moon_nextnew, true),
'hours' => timeleft($astro_moon_nextnew, false, true),
'minutes' => timeleft($astro_moon_nextnew, false, false, true)
]
]
]
];
$arr_astromoo = [
'sun' => $arr_astro_sun,
'moon' => $arr_astro_moon
];
}
/*
WORK WITH THE ARRAYS
*/
$arr = [
'apikey' => (!isset($_GET['key']) ? 'API key provided by Serenum.' : $apikey),
'latitude' => (float)$latitude,
'longitude' => (float)$longitude,
'timezone' => [
'timezone' => $timezone,
'offset' => $timezone_offset,
'gmt' => $datetime->format('O'),
'abbreviation' => $datetime->format('T')
],
'datetime' => [
'local_timestamp' => $timestamp,
'local_date' => date_($timestamp, 'date'),
'local_time' => [
'twentyfourhours' => date_($timestamp, 'time-24h-s'),
'twelvehours' => date_($timestamp, 'time-12h-s')
],
'details' => [
'year' => (int)date('Y', $timestamp),
'month' => [
'number' => (int)date('n', $timestamp),
'name' => [
'full' => date('F', $timestamp),
'short' => date('M', $timestamp)
]
],
'day' => [
'number' => (int)date('j', $timestamp),
'suffix' => date('S', $timestamp),
'weekday' => [
'full' => date('l', $timestamp),
'short' => date('D', $timestamp)
]
],
'week' => (int)date('W', $timestamp)
]
]
];
if(!in_array('place', $arr_excluded)) {
$arr += [
'place' => $arr_place
];
}
$arr += [
'weather' => [
'current' => $arr_weather,
'minutely' => $arr_wea_minutely,
'hourly' => $arr_wea_hourly,
'daily' => $arr_wea_daily,
'alerts' => (empty($arr_alerts) ? null : $arr_alerts)
]
];
if(!in_array('astronomy', $arr_excluded)) {
$arr += [
'astronomy' => $arr_astromoo
];
}
}
}
echo json_encode($arr);
?>