天气查询 全国天气查询,支持全国市区县天气查询,中国气象局实时数据 接口标识: Weather 接口状态: 正常 模块状态: 正常 数据来源: 官方 计费模式: 按次 免KEY调用: 不允许 所属分类: 生活服务 开通 (开通后可享无并发限制, 及KEY相关功能设置)
接口地址:
返回格式:
请求方式:
请求示例:
天气预报 天气查询 天气api
请求参数:
返回参数:
返回示例:
API响应码:
系统响应码:
#1: 10开头 为系统级响应码 、11开头 为API模块级响应码。后面数字代表具体位置
代码示例:
C#
//using System.IO; //using System.Text; //using System.Net; //using System.Net.Security; //using System.Security.Cryptography.X509Certificates; private const String apiurl = "https://u.api.enetapi.com/api/v2/"; //统一请求URL无需修改 private const String alias = "Weather/city"; //接口唯一标识/模块 private const String key = "KEY"; //自己的KEY private const String method = "GET"; static void Main(string[] args) { String params = "&code=500101"; //参数 String bodys = ""; String url = apiurl + alias; HttpWebRequest httpRequest = null; HttpWebResponse httpResponse = null; if (0 < params.Length) { url = url + "?key="+ key + params; } if (apiurl.Contains("https://")) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); } else { httpRequest = (HttpWebRequest)WebRequest.Create(url); } httpRequest.Method = method; if (0 < bodys.Length) { byte[] data = Encoding.UTF8.GetBytes(bodys); using (Stream stream = httpRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); } catch (WebException ex) { httpResponse = (HttpWebResponse)ex.Response; } Console.WriteLine(httpResponse.StatusCode); Console.WriteLine(httpResponse.Method); Console.WriteLine(httpResponse.Headers); Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine("\n"); } public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }
PHP
<?php $apiurl = "https://u.api.enetapi.com/api/v2/"; //统一请求URL无需修改 $alias = "Weather/city"; //接口唯一标识/模块 $key = "KEY"; //自己的KEY $method = "GET"; $headers = array(); $params = "&code=500101"; //参数 $bodys = ""; $url = $apiurl . $alias . "?key=" .$key. $params; $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, false); if (1 == strpos("$".$apiurl, "https://")) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } print_r(curl_exec($curl)); ?>
Python
import urllib, urllib2, sys import ssl apiurl = 'https://u.api.enetapi.com/api/v2/' //统一请求URL无需修改 alias = 'Weather/city' //接口唯一标识/模块 key = 'KEY' //自己的KEY params = '&code=500101' //参数 method = 'GET' bodys = {} url = apiurl + alias + '?key=' + key + params request = urllib2.Request(url) ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE response = urllib2.urlopen(request, context=ctx) content = response.read() if (content): print(content)
ObjectC
NSString *apiurl = @"https://u.api.enetapi.com/api/v2/"; //统一请求URL无需修改 NSString *alias = @"Weather/city"; //接口唯一标识/模块 NSString *key = @"?KEY"; //自己的KEY NSString *params = @"&code=500101"; //参数 NSString *method = @"GET"; NSString *url = [NSString stringWithFormat:@"%@%@%@", apiurl, alias ,key , params]; NSString *bodys = @""; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url] cachePolicy:1 timeoutInterval: 5]; request.HTTPMethod = method; NSURLSession *requestSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; NSURLSessionDataTask *task = [requestSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable body , NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"Response object: %@" , response); NSString *bodyString = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]; //打印body NSLog(@"Response body: %@" , bodyString); }]; [task resume];
CURL
curl -i -k --get --include 'https://u.api.enetapi.com/api/v2/Weather/city?code=500101&key=自己的KEY'