Copy the code below:
<?php
// Example coordinates (New York City)
$latitude = 40.7128;
$longitude = -74.0060;
// Build API URL
$url = "https://api.open-meteo.com/v1/forecast?latitude=$latitude&longitude=$longitude¤t_weather=true";
// Call API
$response = file_get_contents($url);
if ($response !== false) {
$data = json_decode($response, true);
if (isset($data['current_weather'])) {
$weather = $data['current_weather'];
$temperature = $weather['temperature'];
$windspeed = $weather['windspeed'];
$weathercode = $weather['weathercode'];
} else {
$error = "Weather data not available.";
}
} else {
$error = "Could not connect to weather service.";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Weather App - Open Meteo</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
padding: 40px;
}
.card {
background: white;
padding: 20px;
width: 300px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<div class="card">
<h2>Current Weather - </h2>
<?php if (isset($error)): ?>
<p><?php echo $error; ?></p>
<?php else: ?>
<p><strong>Temperature:</strong> <?php echo $temperature; ?>°C</p>
<p><strong>Wind Speed:</strong> <?php echo $windspeed; ?> km/h</p>
<p><strong>Weather Code:</strong> <?php echo $weathercode; ?></p>
<?php endif; ?>
</div>
</body>
</html>