82 lines
3.8 KiB
PHP
82 lines
3.8 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// --- OpenWeatherMap API Call ---
|
|
$openweathermap_api_key = 'ff101be91e4bbe53d6ffbbec1868dfc0';
|
|
$api_url = "https://api.openweathermap.org/data/3.0/onecall?lat=35.2271&lon=-80.8431&units=imperial&exclude=minutely,hourly&appid={$openweathermap_api_key}";
|
|
|
|
$response = @file_get_contents($api_url);
|
|
if ($response === false) {
|
|
http_response_code(502); // Bad Gateway
|
|
echo json_encode(['error' => 'Failed to fetch data from OpenWeatherMap API.']);
|
|
exit;
|
|
}
|
|
|
|
$weather_data = json_decode($response, true);
|
|
|
|
if ($weather_data === null || !isset($weather_data['current'])) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Invalid response from OpenWeatherMap API.', 'details' => $weather_data]);
|
|
exit;
|
|
}
|
|
|
|
// --- Data Transformation & Storage ---
|
|
$current_weather = $weather_data['current'];
|
|
$temperature_f = $current_weather['temp'];
|
|
$is_extreme_heat = ($temperature_f > 95);
|
|
$is_extreme_cold = ($temperature_f < 32);
|
|
|
|
$weather_record = [
|
|
':location_name' => "Charlotte, NC",
|
|
':zip_code' => "28202",
|
|
':observation_time' => date('Y-m-d H:i:s', $current_weather['dt'] ?? time()),
|
|
':weather_condition' => $current_weather['weather'][0]['main'] ?? null,
|
|
':weather_description' => $current_weather['weather'][0]['description'] ?? null,
|
|
':weather_icon' => $current_weather['weather'][0]['icon'] ?? null,
|
|
':temperature_f' => $temperature_f,
|
|
':feels_like_f' => $current_weather['feels_like'] ?? null,
|
|
':temp_min_f' => $weather_data['daily'][0]['temp']['min'] ?? null, // Approximating from daily
|
|
':temp_max_f' => $weather_data['daily'][0]['temp']['max'] ?? null, // Approximating from daily
|
|
':humidity_pct' => $current_weather['humidity'] ?? null,
|
|
':wind_speed_mph' => $current_weather['wind_speed'] ?? null,
|
|
':is_extreme_heat' => $is_extreme_heat ? 1 : 0,
|
|
':is_extreme_cold' => $is_extreme_cold ? 1 : 0,
|
|
':is_severe_weather' => isset($weather_data['alerts']) ? 1 : 0,
|
|
':weather_alerts' => isset($weather_data['alerts']) ? json_encode($weather_data['alerts']) : null,
|
|
];
|
|
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO weather (zip_code, location_name, observation_time, weather_condition, weather_description, weather_icon, temperature_f, feels_like_f, temp_min_f, temp_max_f, humidity_pct, wind_speed_mph, is_extreme_heat, is_extreme_cold, is_severe_weather, weather_alerts)
|
|
VALUES (:zip_code, :location_name, :observation_time, :weather_condition, :weather_description, :weather_icon, :temperature_f, :feels_like_f, :temp_min_f, :temp_max_f, :humidity_pct, :wind_speed_mph, :is_extreme_heat, :is_extreme_cold, :is_severe_weather, :weather_alerts)
|
|
ON DUPLICATE KEY UPDATE
|
|
location_name = VALUES(location_name),
|
|
observation_time = VALUES(observation_time),
|
|
weather_condition = VALUES(weather_condition),
|
|
weather_description = VALUES(weather_description),
|
|
weather_icon = VALUES(weather_icon),
|
|
temperature_f = VALUES(temperature_f),
|
|
feels_like_f = VALUES(feels_like_f),
|
|
temp_min_f = VALUES(temp_min_f),
|
|
temp_max_f = VALUES(temp_max_f),
|
|
humidity_pct = VALUES(humidity_pct),
|
|
wind_speed_mph = VALUES(wind_speed_mph),
|
|
is_extreme_heat = VALUES(is_extreme_heat),
|
|
is_extreme_cold = VALUES(is_extreme_cold),
|
|
is_severe_weather = VALUES(is_severe_weather),
|
|
weather_alerts = VALUES(weather_alerts)
|
|
");
|
|
|
|
$stmt->execute($weather_record);
|
|
|
|
// Return the fresh data
|
|
echo json_encode($weather_record);
|
|
|
|
} catch (PDOException $e) {
|
|
error_log("DB Error: " . $e->getMessage());
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Database error while saving weather data.']);
|
|
} |