首頁  >  文章  >  後端開發  >  如何使用PHP和OpenWeatherMap API取得天氣數據

如何使用PHP和OpenWeatherMap API取得天氣數據

WBOY
WBOY原創
2023-06-19 15:06:013535瀏覽

隨著氣候變遷的加劇,天氣預報在我們日常生活中變得愈發重要。而如何在自己的網站上取得準確的天氣預報資料呢? PHP和OpenWeatherMap API是兩種受歡迎的選擇。以下將介紹如何使用PHP和OpenWeatherMap API取得天氣資料。

1.取得API金鑰

使用OpenWeatherMap API需要取得API金鑰。註冊一個帳號並取得API金鑰是完全免費的。登入OpenWeatherMap網站(https://openweathermap.org/)並報名。在個人資料頁面,前往API金鑰標籤並點擊「產生金鑰」按鈕。然後你就可以拿到API金鑰了。

2.建立PHP檔案

接下來是寫程式碼。打開你的Code editor並建立一個PHP文件,例如weather.php。首先,您需要引入OpenWeatherMap API庫。可以透過在頭部添加以下程式碼來實現:

$httpRequestURI = 'https://api.openweathermap.org/data/2.5/weather?q=London&appid={YOUR_API_KEY}';
$response = file_get_contents($httpRequestURI);
$data = json_decode($response);
echo $data->weather[0]->description;

注意,在請求URL中,您需要使用您的API金鑰來取代{YOUR_API_KEY}。

解釋程式碼:

第一行定義要要求的URL位址,其中q=London是查詢您想要搜尋的城市。您可以在這裡替換任何城市的名稱。

第二行使用PHP內建函數file_get_contents()取得回應資料。

第三行將回應資料轉換為JSON格式,並使用json_decode解碼。

第四行中的echo語句輸出所查詢城市的天氣資料。

3.完善程式碼

上述程式碼已經可以實現獲取天氣資料了,但是為了使其更完整和易於理解,下面完善程式碼。

<?php 
if(isset($_POST['city'])) {
    $cityName = $_POST['city'];
    $apikey = "{YOUR_API_KEY}";
    $url = "http://api.openweathermap.org/data/2.5/weather?q=".$cityName."&appid=".$apikey."&lang=zh-cn";
    $data = file_get_contents($url);
    $weatherData = json_decode($data);
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Weather Report Using PHP and OpenWeatherMap API</title>
    <style>
        #card {
            background-color: #F1F1F1;
            padding: 20px;
            border-radius: 10px;
            width: fit-content;
            margin: 0 auto;
            margin-top: 10%;
        }

        .headtext {
            margin: 0;
            padding: 0;
            font-size: 30px;
            font-weight: 500;
            margin-bottom: 10px;
        }

        .subtext {
            margin: 0;
            padding: 0;
            font-size: 20px;
            color: #F44336;
        }

        .data {
            width: 100%;
            margin-top: 20px;
            border-top: 1px solid #000;
            border-collapse: collapse;
        }

        .data th {
            border-bottom: 1px solid #000;
            padding: 10px;
            text-align: left;
        }

        .data td {
            padding: 10px;
        }

        input[type=text]{
            width: 50%;
            border-radius: 5px;
            padding: 10px;
        }

        button {
            padding: 10px;
            background-color: #F44336;
            border: none;
            color: #fff;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div id="card">
        <p class="headtext">天气预报</p>
        <form method="post" action="">
            <input type="text" name="city" placeholder="输入需要查询的城市" />
            <button type="submit">查询</button>
        </form>
        <?php if(isset($weatherData)) { ?>
            <table class="data">
                <tr>
                    <th>城市名</th>
                    <th>天气状况</th>
                    <th>温度</th>
                    <th>风速</th>
                </tr>
                <tr>
                    <td><?php echo $weatherData->name; ?></td>
                    <td><?php echo $weatherData->weather[0]->description; ?></td>
                    <td><?php echo $weatherData->main->temp; ?> &#8451;</td>
                    <td><?php echo $weatherData->wind->speed; ?> m/s</td>
                </tr>
            </table>
        <?php } else { ?>
             <p class="subtext">请输入城市名以获取天气状况。</p>
        <?php } ?>
    </div>
</body>
</html>

這是一個更完整的天氣預報程序,其中包含了以下部分:

#1.判斷用戶是否輸入城市名,如果是則查詢所輸入城市的天氣,如果沒有則輸出提示訊息。

2.在表格中顯示所查詢城市的天氣數據,包括名稱、天氣狀況、溫度和風速。

3.使用CSS樣式美化頁面。

4.結尾

實作使用PHP和OpenWeatherMap API取得天氣資料就到這裡。只要註冊一個OpenWeatherMap帳號,就能輕鬆取得API金鑰並開始取得即時的天氣資料。無論您是要建立一個網站、應用程序,甚至是需要天氣資料來幫助您進行規劃,這些步驟都將幫助您輕鬆地實現您的目標。

以上是如何使用PHP和OpenWeatherMap API取得天氣數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn