🌡️

WeatherAPI.com

已测试可用 GET
天气与环境 提供商: WeatherAPI 需要API Key

功能丰富的天气API,提供实时天气、天气预报、天文数据(日出日落)、时区查询、体育赛事天气等。支持IP自动定位、多语言、空气质量指数。每天100万次免费请求,响应速度快,数据准确。

WeatherAPI.com 是由 WeatherAPI 提供的天气与环境类免费 API,支持 GET 请求方式,需要 API Key 认证。本文档提供完整的调用方法、代码示例(JavaScript/Python/cURL)和常见问题解答。

快速摘要

API 端点: https://api.weatherapi.com/v1/current.json
请求方式: GET
是否免费: 完全免费
认证要求: 需要 API Key

🔗 请求地址

GET Endpoint
https://api.weatherapi.com/v1/current.json

功能特性

需要 API Key 认证
RESTful API
JSON 响应格式

🎯 适用场景

天气预报应用

适合天气与环境相关的天气预报应用项目

出行规划系统

适合天气与环境相关的出行规划系统项目

农业气象监测

适合天气与环境相关的农业气象监测项目

📖 集成指南

1

获取 API 端点

端点地址: https://api.weatherapi.com/v1/current.json

2

添加认证信息

在请求头中添加 API Key

3

解析响应数据

响应为 JSON 格式,根据业务需求处理数据

💻 请求参数

参数名 类型 必填 描述
q string 查询关键字
limit number 返回数量限制

💻 代码示例

JavaScript (Fetch)
浏览器 / Node.js
// JavaScript - 使用 fetch 调用 WeatherAPI.com
async function callAPI() {
  try {
    const response = await fetch('https://api.weatherapi.com/v1/current.json', {
      method: 'GET',
      headers: {
        'Authorization': 'Bearer YOUR_API_KEY',
        'Content-Type': 'application/json'
      }
    });
    const data = await response.json();
    console.log(data);
    return data;
  } catch (error) {
    console.error('API调用失败:', error);
  }
}
🐍Python (Requests)
Python 3.x
# Python - 使用 requests 调用 WeatherAPI.com
import requests

def call_api():
    url = "https://api.weatherapi.com/v1/current.json"
    headers = {
        "Authorization": "Bearer YOUR_API_KEY",
        "Content-Type": "application/json"
    }
    
    try:
        response = requests.get(url, headers=headers)
        data = response.json()
        print(data)
        return data
    except Exception as e:
        print(f"API调用失败: {e}")
cURL
命令行
# cURL - 命令行调用 WeatherAPI.com
curl -X GET "https://api.weatherapi.com/v1/current.json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json"

响应示例

JSON 响应格式 200 OK
{
  "status": "success",
  "data": {
    // 根据WeatherAPI.com的具体功能返回相应数据
    // 请参考官方文档获取完整的响应字段说明
  },
  "message": "请求成功"
}

常见问题

Q: WeatherAPI.com 如何调用?

调用 WeatherAPI.com 需要向 https://api.weatherapi.com/v1/current.json 发送 GET 请求。需要在请求头中添加 API Key 进行认证。

Q: WeatherAPI.com 返回什么数据格式?

WeatherAPI.com 返回 JSON 格式的数据,具体字段请参考官方文档或查看代码示例中的响应结构。

Q: WeatherAPI.com 有调用限制吗?

关于调用限制,请参考 WeatherAPI 的官方文档。建议在代码中实现错误处理和重试机制。