36092-vm/weather.php
Flatlogic Bot 301e490568 op
2025-11-23 07:47:10 +00:00

115 lines
4.9 KiB
PHP

<?php
header('Content-Type: application/json');
function getWeatherByCity($city, $units = 'metric') {
require_once __DIR__ . '/db/config.php';
$apiKey = getenv('OPENWEATHERMAP_API_KEY') ?: '2350ecafed26b115b1557305d956630d';
if ($apiKey === 'YOUR_OPENWEATHERMAP_API_KEY') {
return ['error' => 'Please set your OpenWeatherMap API key in weather.php'];
}
$apiUrl = "https://api.openweathermap.org/data/2.5/weather?q=" . urlencode($city) . "&units={$units}&appid={$apiKey}";
$response = @file_get_contents($apiUrl);
if ($response === FALSE) {
if (strpos($http_response_header[0], "404 Not Found") !== false) {
return ['error' => 'City not found.'];
} else {
return ['error' => 'Failed to get weather data from OpenWeatherMap.'];
}
} else {
$data = json_decode($response, true);
if ($data && !isset($data['error'])) {
// Fetch AQI data
if (isset($data['coord']['lat']) && isset($data['coord']['lon'])) {
$lat = $data['coord']['lat'];
$lon = $data['coord']['lon'];
// Fetch AQI data
$aqiApiUrl = "http://api.openweathermap.org/data/2.5/air_pollution?lat={$lat}&lon={$lon}&appid={$apiKey}";
$aqiResponse = @file_get_contents($aqiApiUrl);
if ($aqiResponse !== FALSE) {
$aqiData = json_decode($aqiResponse, true);
if ($aqiData && isset($aqiData['list'][0]['main']['aqi'])) {
$data['aqi'] = $aqiData['list'][0]['main']['aqi'];
}
}
// Fetch 5-day forecast
$forecastApiUrl = "https://api.openweathermap.org/data/2.5/forecast?lat={$lat}&lon={$lon}&units={$units}&appid={$apiKey}";
$forecastResponse = @file_get_contents($forecastApiUrl);
if ($forecastResponse !== FALSE) {
$forecastData = json_decode($forecastResponse, true);
if ($forecastData && isset($forecastData['list'])) {
$dailyForecast = [];
foreach ($forecastData['list'] as $forecast) {
$day = date('Y-m-d', $forecast['dt']);
// Use the forecast around noon for the daily summary
if (strpos($forecast['dt_txt'], '12:00:00') !== false) {
$dailyForecast[$day] = [
'temp' => $forecast['main']['temp'],
'description' => $forecast['weather'][0]['description'],
'icon' => $forecast['weather'][0]['icon']
];
}
}
$data['forecast'] = array_values($dailyForecast);
}
}
}
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO weather_history (city_name, temperature, humidity, description, icon) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([
$data['name'],
$data['main']['temp'],
$data['main']['humidity'],
$data['weather'][0]['description'],
$data['weather'][0]['icon']
]);
} catch (PDOException $e) {
// Log error, but don't block the user
error_log("Could not save weather history: " . $e->getMessage());
}
}
return $data;
}
}
if (isset($_GET['city'])) {
$units = $_GET['units'] ?? 'metric';
echo json_encode(getWeatherByCity($_GET['city'], $units));
exit;
}
// The rest of the file remains the same for lat/lon requests
$lat = $_GET['lat'] ?? null;
$lon = $_GET['lon'] ?? null;
if ($lat && $lon) {
$units = $_GET['units'] ?? 'metric';
$apiKey = getenv('OPENWEATHERMAP_API_KEY') ?: '2350ecafed26b115b1557305d956630d';
$apiUrl = "https://api.openweathermap.org/data/2.5/weather?lat={$lat}&lon={$lon}&units={$units}&appid={$apiKey}";
$response = @file_get_contents($apiUrl);
if ($response === FALSE) {
echo json_encode(['error' => 'Failed to get weather data from OpenWeatherMap.']);
} else {
$data = json_decode($response, true);
// Fetch AQI data
$aqiApiUrl = "http://api.openweathermap.org/data/2.5/air_pollution?lat={$lat}&lon={$lon}&appid={$apiKey}";
$aqiResponse = @file_get_contents($aqiApiUrl);
if ($aqiResponse !== FALSE) {
$aqiData = json_decode($aqiResponse, true);
if ($aqiData && isset($aqiData['list'][0]['main']['aqi'])) {
$data['aqi'] = $aqiData['list'][0]['main']['aqi'];
}
}
echo json_encode($data);
}
} else {
echo json_encode(['error' => 'City or latitude and longitude are required.']);
}