什么是當(dāng)前天氣 API?

它是一個(gè)應(yīng)用程序編程接口,可用于將實(shí)時(shí)天氣數(shù)據(jù)集成到您的應(yīng)用程序和網(wǎng)站中。一些高效的 API(如 Weatherstack)還可以為您提供歷史天氣數(shù)據(jù)和準(zhǔn)確的天氣預(yù)報(bào)。此外,您還可以使用這些 API 來(lái)顯示不同使用場(chǎng)景的天氣狀況。

例如,一個(gè)徒步旅行應(yīng)用程序可以收集不同路徑位置的天氣預(yù)報(bào)數(shù)據(jù)并顯示給用戶(hù)。同樣,建筑行業(yè)可以使用天氣 API 來(lái)預(yù)測(cè)風(fēng)速、降雨量和溫度,并做出相應(yīng)的計(jì)劃。

您應(yīng)該使用哪種天氣 API?

選擇 API 時(shí)要考慮的最重要因素是數(shù)據(jù)準(zhǔn)確性。這是因?yàn)椴粶?zhǔn)確的數(shù)據(jù)會(huì)極大地影響您的計(jì)劃和業(yè)務(wù)活動(dòng)。由于天氣 API 使用不同的來(lái)源來(lái)收集數(shù)據(jù),因此它們的準(zhǔn)確性也不同。如果您正在尋找高度可靠的 API,Weathertstack 是最佳選擇。

Weatherstack 是一個(gè)天氣 API,受到全球 75,000 多家公司的信任,包括 Microsoft 和 Ericsson。該API 不僅提供準(zhǔn)確的實(shí)時(shí)天氣數(shù)據(jù),還可以使用它來(lái)獲取歷史天氣數(shù)據(jù)和準(zhǔn)確的天氣預(yù)報(bào)。除了高度準(zhǔn)確之外,Weatherstack 還以其用戶(hù)友好性和可靠性而聞名。

weatherstack 當(dāng)前天氣 API 獲取最準(zhǔn)確的天氣數(shù)據(jù),例如用于惡劣天氣警報(bào)的全球天氣數(shù)據(jù)

Weatherstack 提供哪些功能?

Weatherstack 具有廣泛的有價(jià)值的功能,它們是領(lǐng)先的天氣 API。讓我們來(lái)探索一下它們:

支持的位置

Weatherstack 當(dāng)前天氣 API 涵蓋全球當(dāng)前、歷史和天氣預(yù)報(bào)數(shù)據(jù) – 您可以使用該 API 查找全球數(shù)百萬(wàn)個(gè)城鎮(zhèn)的天氣狀況。當(dāng)前的天氣 API 還支持 40 種口語(yǔ)。

實(shí)時(shí)天氣數(shù)據(jù)

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

以下是當(dāng)前天氣數(shù)據(jù)的 API 調(diào)用示例:

https://api.weatherstack.com/current

? access_key = YOUR_ACCESS_KEY

    & query = New York

上述 API 請(qǐng)求將產(chǎn)生以下結(jié)果:

{

    "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

    }

}

歷史數(shù)據(jù)和天氣預(yù)報(bào)

您還可以使用 Weatherstack 的當(dāng)前天氣 API 來(lái)獲取歷史天氣數(shù)據(jù),以分析過(guò)去的事件或獲得準(zhǔn)確的天氣預(yù)報(bào)。

當(dāng)涉及到歷史數(shù)據(jù)時(shí),您可以使用Weatherstack歷史端點(diǎn)來(lái)獲取2008年的天氣數(shù)據(jù)。您還可以使用 historical_date_start 和 historical_date_end 參數(shù)來(lái)獲取歷史時(shí)間序列結(jié)果。當(dāng)您請(qǐng)求歷史天氣數(shù)據(jù)時(shí),Weatherstack 還會(huì)自動(dòng)返回用于請(qǐng)求的位置的當(dāng)前天氣數(shù)據(jù)。

以下是歷史天氣數(shù)據(jù)的 API 調(diào)用示例:

https://api.weatherstack.com/historical

? access_key = YOUR_ACCESS_KEY

    & query = New York

    & historical_date = 2015-01-21

    & hourly = 1

上述 API 請(qǐng)求將產(chǎn)生以下結(jié)果:

{

    "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

            ]

        }

    }

}

在天氣預(yù)報(bào)方面,Weatherstack 可以提供未來(lái)長(zhǎng)達(dá) 14 天的天氣預(yù)報(bào)數(shù)據(jù)。您還可以獲取每小時(shí)數(shù)據(jù),或定義 3、6、12 或 24 小時(shí)的間隔。

以下是天氣預(yù)報(bào)的 API 調(diào)用示例:

https://api.weatherstack.com/forecast

? access_key = YOUR_ACCESS_KEY

    & query = New York

    & forecast_days = 1

    & hourly = 1

上面的代碼將產(chǎn)生以下結(jié)果:

{

    "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

            ]

        }

    }

}

準(zhǔn)確可靠

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

天文和月球數(shù)據(jù)

您也可以使用 Weatherstack 當(dāng)前天氣 API 來(lái)獲取準(zhǔn)確的天文數(shù)據(jù)。該 API 總共返回 6 個(gè)不同的子響應(yīng)對(duì)象,其中包含天文天氣詳細(xì)信息。這些包括日出時(shí)間、日落時(shí)間、月出時(shí)間、月落時(shí)間、月相(新月、盈新月、上弦月等)和月亮照明級(jí)別。

易于使用

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

Weatherstack 支持哪些編程語(yǔ)言?

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

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

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 可以免費(fèi)使用嗎?

Weatherstack 提供了幾個(gè)軟件包來(lái)查找您選擇的任何位置的天氣狀況。這些套餐范圍從免費(fèi)到付費(fèi)訂閱計(jì)劃。免費(fèi)套餐每月為您提供 250 次通話(huà),并支持當(dāng)前天氣數(shù)據(jù)。

Weatherstack 還提供三種付費(fèi)套餐:

當(dāng)前天氣 API:結(jié)論

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

常見(jiàn)問(wèn)題

1. Weather API 有什么作用?

它為您提供某個(gè)地點(diǎn)的最新天氣信息,例如溫度、濕度和風(fēng)速。

2. Weather API 有多好?

API 的準(zhǔn)確性取決于它從何處獲取數(shù)據(jù)以及更新頻率。它通常使用可靠的來(lái)源(如氣象機(jī)構(gòu))獲取信息。

3. 如何使用 Weatherstack?

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

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

它可以用于應(yīng)用程序、網(wǎng)站或小工具中,向用戶(hù)展示現(xiàn)在的天氣情況。在農(nóng)業(yè)或運(yùn)輸?shù)阮I(lǐng)域,根據(jù)當(dāng)前天氣規(guī)劃活動(dòng)也很有幫助。

5. 如何找到更多同類(lèi)API?

冪簡(jiǎn)集成是國(guó)內(nèi)領(lǐng)先的API集成管理平臺(tái),專(zhuān)注于為開(kāi)發(fā)者提供全面、高效、易用的API集成解決方案。冪簡(jiǎn)API平臺(tái)可以通過(guò)以下兩種方式找到所需API:通過(guò)關(guān)鍵詞搜索API、或者從API Hub分類(lèi)頁(yè)進(jìn)入尋找。

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

上一篇:

使用 IP Geolocator 的 10 大網(wǎng)站示例

下一篇:

使用 CloudFormation 在 AWS API 網(wǎng)關(guān)上設(shè)置 CORS 和集成
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門(mén)場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

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

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)