什么是當前天氣 API?

它是一個應用程序編程接口,可用于將實時天氣數據集成到您的應用程序和網站中。一些高效的 API(如 Weatherstack)還可以為您提供歷史天氣數據和準確的天氣預報。此外,您還可以使用這些 API 來顯示不同使用場景的天氣狀況。

例如,一個徒步旅行應用程序可以收集不同路徑位置的天氣預報數據并顯示給用戶。同樣,建筑行業(yè)可以使用天氣 API 來預測風速、降雨量和溫度,并做出相應的計劃。

您應該使用哪種天氣 API?

選擇 API 時要考慮的最重要因素是數據準確性。這是因為不準確的數據會極大地影響您的計劃和業(yè)務活動。由于天氣 API 使用不同的來源來收集數據,因此它們的準確性也不同。如果您正在尋找高度可靠的 API,Weathertstack 是最佳選擇。

Weatherstack 是一個天氣 API,受到全球 75,000 多家公司的信任,包括 Microsoft 和 Ericsson。該API 不僅提供準確的實時天氣數據,還可以使用它來獲取歷史天氣數據和準確的天氣預報。除了高度準確之外,Weatherstack 還以其用戶友好性和可靠性而聞名。

weatherstack 當前天氣 API 獲取最準確的天氣數據,例如用于惡劣天氣警報的全球天氣數據

Weatherstack 提供哪些功能?

Weatherstack 具有廣泛的有價值的功能,它們是領先的天氣 API。讓我們來探索一下它們:

支持的位置

Weatherstack 當前天氣 API 涵蓋全球當前、歷史和天氣預報數據 – 您可以使用該 API 查找全球數百萬個城鎮(zhèn)的天氣狀況。當前的天氣 API 還支持 40 種口語。

實時天氣數據

作為當前的天氣 API,Weatherstack 可為您選擇的任何位置提供準確的實時數據。要獲取實時天氣數據,您需要將所需位置附加到 API 的當前終端節(jié)點。根據您的訂閱,API 還允許您創(chuàng)建批量位置 – 您需要將多個以分號分隔的位置傳遞給 API URL

以下是當前天氣數據的 API 調用示例:

https://api.weatherstack.com/current

? access_key = YOUR_ACCESS_KEY

    & query = New York

上述 API 請求將產生以下結果:

{

    "request": {

        "type": "City",

        "query": "New York, United States of America",

        "language": "en",

        "unit": "m"

    },

    "location": {

        "name": "New York",

        "country": "United States of America",

        "region": "New York",

        "lat": "40.714",

        "lon": "-74.006",

        "timezone_id": "America/New_York",

        "localtime": "2019-09-07 08:14",

        "localtime_epoch": 1567844040,

        "utc_offset": "-4.0"

    },

    "current": {

        "observation_time": "12:14 PM",

        "temperature": 13,

        "weather_code": 113,

        "weather_icons": [

            "https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"

        ],

        "weather_descriptions": [

            "Sunny"

        ],

        "wind_speed": 0,

        "wind_degree": 349,

        "wind_dir": "N",

        "pressure": 1010,

        "precip": 0,

        "humidity": 90,

        "cloudcover": 0,

        "feelslike": 13,

        "uv_index": 4,

        "visibility": 16

    }

}

歷史數據和天氣預報

您還可以使用 Weatherstack 的當前天氣 API 來獲取歷史天氣數據,以分析過去的事件或獲得準確的天氣預報。

當涉及到歷史數據時,您可以使用Weatherstack歷史端點來獲取2008年的天氣數據。您還可以使用 historical_date_start 和 historical_date_end 參數來獲取歷史時間序列結果。當您請求歷史天氣數據時,Weatherstack 還會自動返回用于請求的位置的當前天氣數據。

以下是歷史天氣數據的 API 調用示例:

https://api.weatherstack.com/historical

? access_key = YOUR_ACCESS_KEY

    & query = New York

    & historical_date = 2015-01-21

    & hourly = 1

上述 API 請求將產生以下結果:

{

    "request": {

        "type": "City",

        "query": "New York, United States of America",

        "language": "en",

        "unit": "m"

    },

    "location": {

        "name": "New York",

        "country": "United States of America",

        "region": "New York",

        "lat": "40.714",

        "lon": "-74.006",

        "timezone_id": "America/New_York",

        "localtime": "2019-09-07 10:05",

        "localtime_epoch": 1567850700,

        "utc_offset": "-4.0"

    },

    "current": {

        "observation_time": "02:05 PM",

        "temperature": 15,

        "weather_code": 113,

        "weather_icons": [

            "https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"

        ],

        "weather_descriptions": [

            "Sunny"

        ],

        "wind_speed": 0,

        "wind_degree": 0,

        "wind_dir": "N",

        "pressure": 1011,

        "precip": 0,

        "humidity": 78,

        "cloudcover": 0,

        "feelslike": 15,

        "uv_index": 5,

        "visibility": 16

    },

"historical": {

        "2008-07-01": {

            "date": "2008-07-01",

            "date_epoch": 1214870400,

            "astro": {

                "sunrise": "05:29 AM",

                "sunset": "08:31 PM",

                "moonrise": "03:24 AM",

                "moonset": "07:37 PM",

                "moon_phase": "Waning Crescent",

                "moon_illumination": 4

            },

            "mintemp": 0,

            "maxtemp": 0,

            "avgtemp": 19,

            "totalsnow": 0,

            "sunhour": 14.5,

            "uv_index": 4,

            "hourly": [

                {

                    "time": "0",

                    "temperature": 27,

                    "wind_speed": 7,

                    "wind_degree": 201,

                    "wind_dir": "SSW",

                    "weather_code": 113,

                    "weather_icons": [

                        "https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"

                    ],

                    "weather_descriptions": [

                        "Sunny"

                    ],

                    "precip": 1.8,

                    "humidity": 80,

                    "visibility": 9,

                    "pressure": 1011,

                    "cloudcover": 15,

                    "heatindex": 25,

                    "dewpoint": 20,

                    "windchill": 24,

                    "windgust": 11,

                    "feelslike": 25,

                    "chanceofrain": 0,

                    "chanceofremdry": 0,

                    "chanceofwindy": 0,

                    "chanceofovercast": 0,

                    "chanceofsunshine": 0,

                    "chanceoffrost": 0,

                    "chanceofhightemp": 0,

                    "chanceoffog": 0,

                    "chanceofsnow": 0,

                    "chanceofthunder": 0,

                    "uv_index": 6

                },

                {   "time": "300", ...   },

                {   "time": "600", ...   },

                // 6 more items

            ]

        }

    }

}

在天氣預報方面,Weatherstack 可以提供未來長達 14 天的天氣預報數據。您還可以獲取每小時數據,或定義 3、6、12 或 24 小時的間隔。

以下是天氣預報的 API 調用示例:

https://api.weatherstack.com/forecast

? access_key = YOUR_ACCESS_KEY

    & query = New York

    & forecast_days = 1

    & hourly = 1

上面的代碼將產生以下結果:

{

    "request": {

        "type": "City",

        "query": "New York, United States of America",

        "language": "en",

        "unit": "m"

    },

    "location": {

        "name": "New York",

        "country": "United States of America",

        "region": "New York",

        "lat": "40.714",

        "lon": "-74.006",

        "timezone_id": "America/New_York",

        "localtime": "2019-09-07 11:38",

        "localtime_epoch": 1567856280,

        "utc_offset": "-4.0"

    },

    "current": {

        "observation_time": "03:38 PM",

        "temperature": 18,

        "weather_code": 113,

        "weather_icons": [

            "https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"

        ],

        "weather_descriptions": [

            "Sunny"

        ],

"wind_speed": 0,

        "wind_degree": 345,

        "wind_dir": "NNW",

        "pressure": 1011,

        "precip": 0,

        "humidity": 58,

        "cloudcover": 0,

        "feelslike": 18,

        "uv_index": 5,

        "visibility": 16

    },

    "forecast": {

        "2019-09-07": {

            "date": "2019-09-07",

            "date_epoch": 1567814400,

            "astro": {

                "sunrise": "06:28 AM",

                "sunset": "07:19 PM",

                "moonrise": "03:33 PM",

                "moonset": "12:17 AM",

                "moon_phase": "First Quarter",

                "moon_illumination": 54

            },

            "mintemp": 17,

            "maxtemp": 25,

            "avgtemp": 21,

            "totalsnow": 0,

            "sunhour": 10.3,

            "uv_index": 5,

            "hourly": [

                {

                    "time": "0",

                    "temperature": 18,

                    "wind_speed": 28,

                    "wind_degree": 15,

                    "wind_dir": "NNE",

                    "weather_code": 122,

                    "weather_icons": [

                        "https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"

                    ],

                    "weather_descriptions": [

                        "Overcast"

                    ],

                    "precip": 0,

                    "humidity": 68,

                    "visibility": 10,

                    "pressure": 1008,

                    "cloudcover": 75,

                    "heatindex": 18,

                    "dewpoint": 12,

                    "windchill": 18,

                    "windgust": 35,

                    "feelslike": 18,

"chanceofrain": 0,

                    "chanceofremdry": 87,

                    "chanceofwindy": 0,

                    "chanceofovercast": 90,

                    "chanceofsunshine": 15,

                    "chanceoffrost": 0,

                    "chanceofhightemp": 0,

                    "chanceoffog": 0,

                    "chanceofsnow": 0,

                    "chanceofthunder": 0,

                    "uv_index": 0

                },

                {   "time": "300", ...   },

                {   "time": "600", ...   },

                // 6 more items

            ]

        }

    }

}

準確可靠

Weatherstack API從全球主要的大型氣象站和供應商等地點來獲取天氣數據。因此,您可以信任 API 為您提供準確和精確的天氣數據,以便為不同的天氣狀況做好準備。此外,Weatherstack 具有 99.9% 的正常運行時間。這使得它高度可靠且一致。Weatherstack API 的另一個優(yōu)點是它可以在幾毫秒內提供天氣數據。

天文和月球數據

您也可以使用 Weatherstack 當前天氣 API 來獲取準確的天文數據。該 API 總共返回 6 個不同的子響應對象,其中包含天文天氣詳細信息。這些包括日出時間、日落時間、月出時間、月落時間、月相(新月、盈新月、上弦月等)和月亮照明級別。

易于使用

Weatherstack API 非常易于使用和集成。它附帶詳細的天氣API文檔,其中包括入門指南和使用不同API功能及終端節(jié)點的分步教程。該文檔還包括幾個常用編程語言的代碼示例。

Weatherstack 支持哪些編程語言?

在編程語言方面,Weatherstack 支持所有主要的編程語言,包括 PHP、Python、Nodejs 和 jQuery。

以下是常用編程語言中的一些代碼示例:

PHP

$location = 'New York';

$queryString = http_build_query([

  'access_key' => 'YOUR_ACCESS_KEY',

  'query' => $location,

]);

$ch = curl_init(sprintf('%s?%s', 'https://api.weatherstack.com/current', $queryString));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$json = curl_exec($ch);

curl_close($ch);

$api_result = json_decode($json, true);

echo "Current temperature in $location is {$api_result['current']['temperature']}℃", PHP_EOL;

Python

# coding: utf-8

import requests

params = {

  'access_key': 'YOUR_ACCESS_KEY',

  'query': 'New York'

}

api_result = requests.get('https://api.weatherstack.com/current', params)

api_response = api_result.json()

print(u'Current temperature in %s is %d℃' % (api_response['location']['name'], api_response['current']['temperature']))

Nodejs

const axios = require('axios');

const params = {

  access_key: 'YOUR_ACCESS_KEY',

  query: 'New York'

}

axios.get('https://api.weatherstack.com/current', {params})

  .then(response => {

    const apiResponse = response.data;

    console.log(Current temperature in ${apiResponse.location.name} is ${apiResponse.current.temperature}℃);

  }).catch(error => {

    console.log(error);

  });

Weatherstack 可以免費使用嗎?

Weatherstack 提供了幾個軟件包來查找您選擇的任何位置的天氣狀況。這些套餐范圍從免費到付費訂閱計劃。免費套餐每月為您提供 250 次通話,并支持當前天氣數據。

Weatherstack 還提供三種付費套餐:

當前天氣 API:結論

天氣影響著我們的日常生活,改變了我們的戶外活動和旅行計劃。企業(yè)還需要了解其運營的天氣。此外,它通過提供實時和過去的天氣數據來提供幫助。Weatherstack 是許多公司使用的值得信賴的 API。它提供全球覆蓋、支持多種語言以及輕松訪問天氣數據等功能。它可靠且易于使用,支持不同的編程語言。Weatherstack 有不同的計劃,包括免費的計劃,因此您可以選擇最適合您的計劃。

常見問題

1. Weather API 有什么作用?

它為您提供某個地點的最新天氣信息,例如溫度、濕度和風速。

2. Weather API 有多好?

API 的準確性取決于它從何處獲取數據以及更新頻率。它通常使用可靠的來源(如氣象機構)獲取信息。

3. 如何使用 Weatherstack?

要使用此 API,您需要獲取一個名為 API 密鑰的特殊代碼。您使用密鑰向 API 發(fā)送請求,它會以 JSON 或 XML 等格式發(fā)回天氣數據。

4. 我可以用 Current Weather API 做什么?

它可以用于應用程序、網站或小工具中,向用戶展示現在的天氣情況。在農業(yè)或運輸等領域,根據當前天氣規(guī)劃活動也很有幫助。

5. 如何找到更多同類API?

冪簡集成是國內領先的API集成管理平臺,專注于為開發(fā)者提供全面、高效、易用的API集成解決方案。冪簡API平臺可以通過以下兩種方式找到所需API:通過關鍵詞搜索API、或者從API Hub分類頁進入尋找。

原文鏈接:https://blog.apilayer.com/a-beginners-guide-to-a-current-weather-api/

上一篇:

使用 IP Geolocator 的 10 大網站示例

下一篇:

使用 CloudFormation 在 AWS API 網關上設置 CORS 和集成
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費