Home > Article > Backend Development > Use PHP and Baidu Map API to display and forecast weather information
Use PHP and Baidu Map API to display and forecast weather information
Introduction:
Weather is a very important part of people's lives, and understanding weather conditions can help us make reasonable decisions. In web development, displaying weather information to users in real time is a valuable function. This article introduces how to use PHP and Baidu Map API to display and forecast weather information, and provides code examples for readers' reference.
The following is a sample code for getting real-time weather data for a certain city:
<?php // 城市名称 $city = "北京"; // 百度地图天气API的请求URL $url = "http://api.map.baidu.com/telematics/v3/weather?location=" . $city . "&output=json&ak=你的密钥"; // 使用CURL发送HTTP请求 $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($curl); curl_close($curl); // 解析JSON格式的响应数据 $data = json_decode($result); // 提取需要的天气信息 $currentWeather = $data->results[0]->weather_data[0]; // 打印天气信息 echo "城市:" . $currentWeather->currentCity . "<br>"; echo "日期:" . $currentWeather->date . "<br>"; echo "天气:" . $currentWeather->weather . "<br>"; echo "风力:" . $currentWeather->wind . "<br>"; echo "温度:" . $currentWeather->temperature . "<br>"; ?>
In the above code, first specify the name of the city to get the weather, and then build Request the URL and replace the developer key (AK) with your own key. An HTTP request is sent using the CURL library and the response result is saved in the $result variable. Then, use the json_decode function to parse the JSON data into a PHP object, extract the required weather information from it, and finally output the result to the web page through the echo statement.
<!DOCTYPE html> <html> <head> <title>天气信息显示</title> <style> .weather-container { width: 400px; margin: 0 auto; text-align: center; } .weather-info { background-color: #f5f5f5; padding: 10px; margin-bottom: 10px; } .weather-info p { margin: 5px; } </style> </head> <body> <div class="weather-container"> <div class="weather-info"> <h2>天气信息</h2> <?php // 在这里插入获取天气数据的代码 echo "<p>城市:" . $currentWeather->currentCity . "</p>"; echo "<p>日期:" . $currentWeather->date . "</p>"; echo "<p>天气:" . $currentWeather->weather . "</p>"; echo "<p>风力:" . $currentWeather->wind . "</p>"; echo "<p>温度:" . $currentWeather->temperature . "</p>"; ?> </div> </div> </body> </html>
In the above code, we use CSS to define a weather-container class to set the style of the container. The weather-info class is used to set the style of weather information. In the PHP code block, we insert the obtained weather information into HTML tags and use the echo statement to output it to the web page.
Summary:
This article introduces how to use PHP and Baidu Map API to display and forecast weather information. By using Baidu Map API, we can easily obtain the weather data we want and display it on the web page in real time. Readers can further expand and optimize this function according to their own needs. Hope this article can be helpful to readers.
The above is the detailed content of Use PHP and Baidu Map API to display and forecast weather information. For more information, please follow other related articles on the PHP Chinese website!