所有文章 > 学习各类API > 当前天气 API | 2024 年完整的初学者指南
当前天气 API | 2024 年完整的初学者指南

当前天气 API | 2024 年完整的初学者指南

天气直接影响我们的生活。例如,降雪、大雨和大风会显著影响户外活动,例如远足或露营。同样,你也不能计划在大雨或降雪的时候去海滩旅行。然而,不仅仅是公众对天气状况感兴趣。气候也对企业和工业产生重大影响。这就是当前天气 API 发挥作用的地方。

Current Weather API 对于预测天气状况和揭示历史趋势都很有价值。此外,公众和企业会跟踪当前和未来的天气状况,以计划和准备他们的活动。使用当前天气 API 是获取准确天气数据的好方法。

为了帮助您了解这些选项,我们创建了这篇关于寻找最佳天气 API 的文章。

什么是当前天气 API?

它是一个应用程序编程接口,可用于将实时天气数据集成到您的应用程序和网站中。一些高效的 API(如 Weatherstack)还可以为您提供历史天气数据和准确的天气预报。此外,您还可以使用这些 API 来显示不同使用场景的天气状况。

例如,一个徒步旅行应用程序可以收集不同路径位置的天气预报数据并显示给用户。同样,建筑行业可以使用天气 API 来预测风速、降雨量和温度,并做出相应的计划。

您应该使用哪种天气 API?

选择 API 时要考虑的最重要因素是数据准确性。这是因为不准确的数据会极大地影响您的计划和业务活动。由于天气 API 使用不同的来源来收集数据,因此它们的准确性也不同。如果您正在寻找高度可靠的 API,Weathertstack 是最佳选择。

Weatherstack 是一个天气 API,受到全球 75,000 多家公司的信任,包括 Microsoft 和 Ericsson。该API 不仅提供准确的实时天气数据,还可以使用它来获取历史天气数据和准确的天气预报。除了高度准确之外,Weatherstack 还以其用户友好性和可靠性而闻名。

weatherstack 当前天气 API 获取最准确的天气数据,例如用于恶劣天气警报的全球天气数据

Weatherstack 提供哪些功能?

Weatherstack 具有广泛的有价值的功能,它们是领先的天气 API。让我们来探索一下它们:

支持的位置

Weatherstack 当前天气 API 涵盖全球当前、历史和天气预报数据 – 您可以使用该 API 查找全球数百万个城镇的天气状况。当前的天气 API 还支持 40 种口语。

实时天气数据

作为当前的天气 API,Weatherstack 可为您选择的任何位置提供准确的实时数据。要获取实时天气数据,您需要将所需位置附加到 API 的当前终端节点。根据您的订阅,API 还允许您创建批量位置 – 您需要将多个以分号分隔的位置传递给 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 的另一个优点是它可以在几毫秒内提供天气数据。

天文和月球数据

您也可以使用 Weatherstack 当前天气 API 来获取准确的天文数据。该 API 总共返回 6 个不同的子响应对象,其中包含天文天气详细信息。这些包括日出时间、日落时间、月出时间、月落时间、月相(新月、盈新月、上弦月等)和月亮照明级别。

易于使用

Weatherstack API 非常易于使用和集成。它附带详细的天气API文档,其中包括入门指南和使用不同API功能及终端节点的分步教程。该文档还包括几个常用编程语言的代码示例。

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 还提供三种付费套餐:

  • 标准:每月 9.99 次 API 调用、实时、历史、每小时和天文数据以及其他一些有用的功能
  • 专业:每月 49.99 次 API 调用、实时、历史、每小时、7 天预报和天文数据、批量查询和许多其他功能
  • 商业:每月 100 万次 API 调用费用为 99.99 USD,包括实时、历史、每小时、14 天预报和天文数据、批量查询和许多其他功能。

当前天气 API:结论

天气影响着我们的日常生活,改变了我们的户外活动和旅行计划。企业还需要了解其运营的天气。此外,它通过提供实时和过去的天气数据来提供帮助。Weatherstack 是许多公司使用的值得信赖的 API。它提供全球覆盖、支持多种语言以及轻松访问天气数据等功能。它可靠且易于使用,支持不同的编程语言。Weatherstack 有不同的计划,包括免费的计划,因此您可以选择最适合您的计划。

常见问题

1. Weather API 有什么作用?

它为您提供某个地点的最新天气信息,例如温度、湿度和风速。

2. Weather API 有多好?

API 的准确性取决于它从何处获取数据以及更新频率。它通常使用可靠的来源(如气象机构)获取信息。

3. 如何使用 Weatherstack?

要使用此 API,您需要获取一个名为 API 密钥的特殊代码。您使用密钥向 API 发送请求,它会以 JSON 或 XML 等格式发回天气数据。

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

它可以用于应用程序、网站或小工具中,向用户展示现在的天气情况。在农业或运输等领域,根据当前天气规划活动也很有帮助。

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

幂简集成是国内领先的API集成管理平台,专注于为开发者提供全面、高效、易用的API集成解决方案。幂简API平台可以通过以下两种方式找到所需API:通过关键词搜索API、或者从API Hub分类页进入寻找。

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

#你可能也喜欢这些API文章!