Home  >  Article  >  Backend Development  >  How to use PHP to develop a simple real-time weather query function

How to use PHP to develop a simple real-time weather query function

PHPz
PHPzOriginal
2023-09-24 12:03:411319browse

How to use PHP to develop a simple real-time weather query function

How to use PHP to develop a simple real-time weather query function

Foreword:
With the continuous development of technology, people are paying more and more attention to the weather many. Therefore, developing a website or application with real-time weather query function has become a very popular demand. This article uses PHP as the development language, introduces how to use PHP to develop a simple real-time weather query function, and provides specific code examples.

1. Obtain weather data
To implement the weather query function, you first need to obtain real-time weather data. There are currently many weather APIs on the market for developers to use, such as Alibaba Cloud Weather API, Zephyr Weather API, etc. These APIs usually provide HTTP interfaces, and weather data can be obtained by sending HTTP requests. In this article, we take the Zephyr Weather API as an example.

  1. Register Zephyr Weather API
    First, you need to register an account on the Zephyr Weather Developer Platform and create an application. After registration is completed, you can get an API Key, which will be used in subsequent code.
  2. Call Zephyr Weather API
    Use PHP’s cURL function to easily send HTTP requests. The following is a sample code to obtain weather data:
<?php
$key = 'your_api_key'; // 替换为您在和风天气平台上获得的API Key
$location = '北京'; // 替换为需要查询的城市名称
$url = 'https://devapi.qweather.com/v7/weather/now?key=' . $key . '&location=' . urlencode($location);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
$weather = $data['now']['text'];
$temperature = $data['now']['temp'];

echo '当前天气:' . $weather . '<br>';
echo '当前温度:' . $temperature . '℃';
?>

In the code, you need to replace your_api_key with the API Key obtained on the Zefeng Weather Platform, BeijingReplace with the name of the city to be queried. Send an HTTP request by calling the curl_exec function. After getting the response, use the json_decode function to parse the weather data in JSON format, then obtain the current weather and temperature, and output them to the page.

2. Design the weather query interface
After obtaining the real-time weather data, you need to design a user interface that allows users to enter the city name and click the query button to obtain weather information.

The following is a simple HTML form used to query the weather:

<!DOCTYPE html>
<html>
<head>
    <title>实时天气查询</title>
</head>
<body>
    <h1>实时天气查询</h1>
    <form action="query.php" method="post">
        <label for="city">城市名称:</label>
        <input type="text" id="city" name="city" required>
        <button type="submit">查询</button>
    </form>
</body>
</html>

In the code, a form is used, and the action attribute of the form specifies sending a query request URL, set it to query.php. After the user enters the city name in the input box and clicks the query button, the form data will be submitted to query.php for processing.

3. Processing Query Request
After receiving the query request, you need to parse the request data and call the Zephyr Weather API to obtain weather data.

The following is a code example for query.php:

<?php
if (!empty($_POST['city'])) {
    $key = 'your_api_key'; // 替换为您在和风天气平台上获得的API Key
    $location = $_POST['city'];
    $url = 'https://devapi.qweather.com/v7/weather/now?key=' . $key . '&location=' . urlencode($location);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $response = curl_exec($ch);
    curl_close($ch);

    $data = json_decode($response, true);
    $weather = $data['now']['text'];
    $temperature = $data['now']['temp'];

    echo '当前城市:' . $location . '<br>';
    echo '当前天气:' . $weather . '<br>';
    echo '当前温度:' . $temperature . '℃';
} else {
    echo '请输入要查询的城市名称。';
}
?>

In the code, first check whether $_POST['city'] is empty , if not empty, perform weather query operation, otherwise output prompt information. The logic of weather query is the same as the previous code for obtaining weather data.

Summary:
This article introduces how to use PHP to develop a simple real-time weather query function, including obtaining weather data, designing the query interface and processing query requests. Through API calls, real-time weather information can be obtained and displayed on the user interface.

The above is the detailed content of How to use PHP to develop a simple real-time weather query function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn