In modern websites or applications, combining real-time weather information can improve the user experience. For example, if you are developing a travel website or outdoor activities platform, information about real-time weather and sunset times is very valuable. This article will explain how to use PHP to combine the weather API to get real-time weather in a certain place and sunset times of the day.
In order to obtain weather information, we need a stable and reliable weather API. This example uses a free API provided by OpenWeather (note: you need to register on the official website first and obtain the API key).
OpenWeather's API provides data containing weather conditions and sunset times. Here is a basic PHP request example:
<?php
$apiKey = 'YOUR_API_KEY'; // Replace with your own API Key
$city = 'Beijing'; // You can change to any supported city
$apiUrl = "https://api.openweathermap.org/data/2.5/weather?q={$city}&appid={$apiKey}&units=metric";
// initialization cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
// decoding JSON
$data = json_decode($response, true);
// Check whether the data is successfully obtained
if (isset($data['main'])) {
$temperature = $data['main']['temp'];
$weather = $data['weather'][0]['description'];
$sunsetUnix = $data['sys']['sunset'];
$sunsetTime = date("H:i:s", $sunsetUnix); // Convert to local time(UTC Time zone adjustment may be required)
echo "Current City:{$city}<br>";
echo "Real-time temperature:{$temperature}°C<br>";
echo "Weather Conditions:{$weather}<br>";
echo "Today's sunset time:{$sunsetTime}<br>";
} else {
echo "Unable to obtain weather information,Please check the city name or API Key是否正确。";
}
?>
Time zone problem : The API returns UTC time, which may need to be converted according to the time zone where your server or user is located. It can be handled using PHP's DateTimeZone class.
Caching strategy : Weather data does not need to be refreshed every second, you can set up a cache, such as update every 10 minutes to relieve API request pressure.
Error handling : Be sure to deal with the failure of API requests to avoid page crashes due to network problems or exhaustion of API quotas.
Interface security : Try not to expose your API keys to the front-end code, and it is best to proxy them through the back-end.
Save the above PHP script as weather.php and deploy it to your server, for example:
https://www.m66.net/weather.php
Visit this address to see weather and sunset time information.
Showing real-time information in combination with the weather API can not only make your website more interactive, but also provide practical help. Hope this article will inspire you! If you have more complex needs, such as querying weather by coordinates, displaying forecasts for the next few days, etc., you can also continue to explore other API interfaces provided by OpenWeather.
Do you want to add it to the existing website or make a module independently?
Related Tags:
API