国内率先完整提供 Demo 代码示例及数据专业且全面的 API 查询接口。
打造精品数据 API 品质,腾讯云提供计算服务及亿百天技术提供技术支持。
注册本站会员,即可免费使用!

天气查询

接口使用

我要查询

提示:请在下面的 Demo 中输入您要查询的数据,点击查询即可。

天气查询(接口一:根据城市/地区名称查询)



天气查询(接口二:根据城市/地区经纬度查询)



经纬度查询(扩展接口:根据城市/地区名称查询对应经纬度)


查询说明

目前支持全球各大城市及地区的天气查询;支持全球各大城市及地区的经纬度查询。支持各级行政区域查询,如:湖北省、武汉市、东西湖区、径河街道、莲花湖村等;当查询的行政区划越小时,精确度可能不高,可能存在重名的情况。

接口描述

功能描述

获取城市/地区的天气信息,比如:天气情况、温度、空气湿度、气压、日出日落时间等天气信息;获取城市/地区对应的经纬度等信息。

URL 示例

接口一:根据城市/地区名称查询

POST 方式请求:

1)http 协议:

http://cha.ebaitian.cn/api/json?appid=xxx&module=getCityWeatherInfo&city=xxx&lang=zh&sign=xxx

GET 方式请求:

http://cha.ebaitian.cn/api/json?type=get&appid=xxx&module=getCityWeatherInfo&city=xxx&lang=zh&sign=xxx

2)https 协议:

https://cha.ebaitian.cn/api/json?appid=xxx&module=getCityWeatherInfo&city=xxx&lang=zh&sign=xxx

GET 方式请求:

https://cha.ebaitian.cn/api/json?type=get&appid=xxx&module=getCityWeatherInfo&city=xxx&lang=zh&sign=xxx

接口二:根据城市/地区经纬度查询

POST 方式请求:

1)http 协议:

http://cha.ebaitian.cn/api/json?appid=xxx&module=getLatLonWeatherInfo&lat=xxx&lon=xxx&lang=zh&sign=xxx

GET 方式请求:

http://cha.ebaitian.cn/api/json?type=get&appid=xxx&module=getLatLonWeatherInfo&lat=xxx&lon=xxx&lang=zh&sign=xxx

2)https 协议:

https://cha.ebaitian.cn/api/json?appid=xxx&module=getLatLonWeatherInfo&lat=xxx&lon=xxx&lang=zh&sign=xxx

GET 方式请求:

https://cha.ebaitian.cn/api/json?type=get&appid=xxx&module=getLatLonWeatherInfo&lat=xxx&lon=xxx&lang=zh&sign=xxx

扩展接口:根据城市/地区名称查询对应经纬度

POST 方式请求:

1)http 协议:

http://cha.ebaitian.cn/api/json?appid=xxx&module=getCityLatLonInfo&city=xxx&lang=zh&sign=xxx

GET 方式请求:

http://cha.ebaitian.cn/api/json?type=get&appid=xxx&module=getCityLatLonInfo&city=xxx&lang=zh&sign=xxx

2)https 协议:

https://cha.ebaitian.cn/api/json?appid=xxx&module=getCityLatLonInfo&city=xxx&lang=zh&sign=xxx

GET 方式请求:

https://cha.ebaitian.cn/api/json?type=get&appid=xxx&module=getCityLatLonInfo&city=xxx&lang=zh&sign=xxx

请求参数

数据包体

接口一:根据城市/地区名称查询

{
    "type": "get",
    "appid": "1000xxxx",
    "module": "getCityWeatherInfo",
    "city": "武汉",
    "lang": "zh",
    "sign": "ecab4881ee80ad3d76bb1da68387428ca752eb885e52621a3129dcf4d9bc4fd4"
}

接口二:根据城市/地区经纬度查询

{
    "type": "get",
    "appid": "1000xxxx",
    "module": "getLatLonWeatherInfo",
    "lat": "30.5951051",
    "lon": "114.2999353",
    "lang": "zh",
    "sign": "ecab4881ee80ad3d76bb1da68387428ca752eb885e52621a3129dcf4d9bc4fd4"
}

扩展接口:根据城市/地区名称查询对应经纬度

{
    "type": "get",
    "appid": "1000xxxx",
    "module": "getCityLatLonInfo",
    "city": "武汉",
    "lang": "zh",
    "sign": "ecab4881ee80ad3d76bb1da68387428ca752eb885e52621a3129dcf4d9bc4fd4"
}

参数说明

接口一:根据城市/地区名称查询

参数 必选 类型 描述
type string 授权接口的请求方式
appid string 授权接口的 AppID,请填写您在我要查官网上申请到的 AppID
module string 目标请求的数据模块,查询手机号码归属地为:getCityWeatherInfo
city string 目标要查询的城市/地区名称,支持全球各个城市及地区的天气查询
lang string 中文请赋值:zh
sign string 请求凭证,具体计算方式见下面的其他说明

接口二:根据城市/地区经纬度查询

参数 必选 类型 描述
type string 授权接口的请求方式
appid string 授权接口的 AppID,请填写您在我要查官网上申请到的 AppID
module string 目标请求的数据模块,查询手机号码归属地为:getLatLonWeatherInfo
lon string 目标要查询的城市/地区经度,支持全球各个城市及地区的天气查询
lat string 目标要查询的城市/地区纬度,支持全球各个城市及地区的天气查询
lang string 中文请赋值:zh
sign string 请求凭证,具体计算方式见下面的其他说明

扩展接口:根据城市/地区名称查询对应经纬度

参数 必选 类型 描述
type string 授权接口的请求方式
appid string 授权接口的 AppID,请填写您在我要查官网上申请到的 AppID
module string 目标请求的数据模块,查询手机号码归属地为:getCityLatLonInfo
city string 目标要查询的城市/地区名称,支持全球各个城市及地区的天气查询
lang string 中文请赋值:zh
sign string 请求凭证,具体计算方式见下面的其他说明

其他说明

1)type:可选值 get,如果赋值 get,则以 get 方式提交数据;默认以 post 方式提交数据;
2)sign:签名校验
当 module 赋值 getCityWeatherInfo 时,根据公式 $sign=sha256(appid=$appid&module=getCityWeatherInfo&city=$city&lang=$lang&appkey=$appkey) 生成;
当 module 赋值 getLatLonWeatherInfo 时,根据公式 $sign=sha256(appid=$appid&module=getLatLonWeatherInfo&lat=$lat&lon=$lon&lang=$lang&appkey=$appkey) 生成;
当 module 赋值 getCityLatLonInfo 时,根据公式 $sign=sha256(appid=$appid&module=getCityLatLonInfo&city=$city&lang=$lang&appkey=$appkey) 生成;
其中:appkey 为授权接口的 AppKey,请填写您在我要查官网上申请到的 AppKey 。

构造伪代码如下:

接口一:根据城市/地区名称查询

string type = "get"; //请求方式,可以赋值为:post
string appid = "1000xxxx"; //sdkappid 对应的 appid,需要业务方高度保密
string module = "getCityWeatherInfo"; //请求的数据模块,此处赋值:getCityWeatherInfo
string city = "武汉"; //要查询的城市/地区名称
string lang = "zh"; //中文请赋值:zh
string sign = sha256(appid=1000xxxx&module=getCityWeatherInfo&city=武汉&lang=zh&appkey=56cf61af4b7897e704f67deb88ae8f24);

接口二:根据城市/地区经纬度查询

string type = "get"; //请求方式,可以赋值为:post
string appid = "1000xxxx"; //sdkappid 对应的 appid,需要业务方高度保密
string module = "getLatLonWeatherInfo"; //请求的数据模块,此处赋值:getLatLonWeatherInfo
string lon = "114.2999353"; //要查询的城市/地区经度
string lat = "30.5951051"; //要查询的城市/地区纬度
string lang = "zh"; //中文请赋值:zh
string sign = sha256(appid=1000xxxx&module=getLatLonWeatherInfo&lat=30.5951051&lon=114.2999353&lang=zh&appkey=56cf61af4b7897e704f67deb88ae8f24);

扩展接口:根据城市/地区名称查询对应经纬度

string type = "get"; //请求方式,可以赋值为:post
string appid = "1000xxxx"; //sdkappid 对应的 appid,需要业务方高度保密
string module = "getCityLatLonInfo"; //请求的数据模块,此处赋值:getCityLatLonInfo
string city = "武汉"; //要查询的城市/地区名称
string lang = "zh"; //中文请赋值:zh
string sign = sha256(appid=1000xxxx&module=getCityLatLonInfo&city=武汉&lang=zh&appkey=56cf61af4b7897e704f67deb88ae8f24);

响应参数

数据包体

接口一:根据城市/地区名称查询

{
  "result": 1,
  "description": "TRUE",
  "flag": "NULL",
  "data": {
    "cityInfo": {
      "name": "Wuhan",
      "lat": 30.5951051,
      "lon": 114.2999353,
      "info": {
        "local_names": {
          "kn": "ವುಹಾನ್",
          "fj": "Wuhan",
          "kg": "Wuhan",
          "fr": "Wuhan",
          "no": "Wuhan",
          "vi": "Vũ Hán",
          "mk": "Вухан",
          "oc": "Wuhan",
          "ta": "வுகான்",
          "ki": "Wuhan",
          "sr": "Вухан",
          "hr": "Wuhan",
          "am": "ዉሃን",
          "ko": "우한시",
          "ascii": "Wuhan",
          "fo": "Wuhan",
          "da": "Wuhan",
          "ky": "Ухань",
          "it": "Wuhan",
          "id": "Wuhan",
          "eo": "Vuhano",
          "gv": "Wuhan",
          "kl": "Wuhan",
          "my": "ဝူဟန့်မြို့",
          "th": "อู่ฮั่น",
          "lv": "Uhaņa",
          "mn": "Үхань",
          "cs": "Wu-chan",
          "bm": "Wuhan",
          "os": "Ухань",
          "ru": "Ухань",
          "la": "Wuhan",
          "zh": "武汉市",
          "eu": "Wuhan",
          "kk": "Ухань",
          "sq": "Vuhani",
          "pl": "Wuhan",
          "ba": "Ухань",
          "ny": "Wuhan",
          "fa": "ووهان",
          "sk": "Wu-chan",
          "el": "Ουχάν",
          "lb": "Wuhan",
          "ku": "Wuhan",
          "br": "Wuhan",
          "hy": "Ուհան",
          "ce": "Ухань",
          "ar": "ووهان",
          "ca": "Wuhan",
          "mi": "Wuhan",
          "kw": "Wuhan",
          "af": "Wuhan",
          "sh": "Wuhan",
          "tn": "Wuhan",
          "az": "Uhan",
          "za": "Vujhan",
          "be": "Ухань",
          "bn": "উহান",
          "ka": "უხანი",
          "tl": "Wuhan",
          "ja": "武漢市",
          "hi": "वूहान",
          "ga": "Wuhan",
          "nl": "Wuhan",
          "is": "Wuhan",
          "lt": "Uhanas",
          "uz": "Uxan",
          "fi": "Wuhan",
          "ug": "ۋۇخەن شەھىرى",
          "bg": "Ухан",
          "mg": "Wuhan",
          "sw": "Wuhan",
          "wo": "Wuhan",
          "ms": "Wuhan",
          "sv": "Wuhan",
          "ha": "Wuhan",
          "uk": "Ухань",
          "he": "ווהאן",
          "mr": "वूहान",
          "pt": "Wuhan",
          "nn": "Wuhan",
          "en": "Wuhan",
          "gl": "Wuhan",
          "tt": "Үһән",
          "es": "Wuhan",
          "ml": "വൂഹാൻ",
          "ro": "Wuhan",
          "qu": "Wuhan",
          "tr": "Vuhan",
          "ur": "ووہان",
          "hu": "Vuhan",
          "pa": "ਵੂਖ਼ਨ",
          "et": "Wuhan",
          "de": "Wuhan",
          "bo": "ཝུའུ་ཧན་གྲོང་ཁྱེར།",
          "cy": "Wuhan",
          "feature_name": "Wuhan"
        },
        "country": "CN",
        "state": "Hubei"
      }
    },
    "weather": {
      "coord": {
        "lon": 114.2999,
        "lat": 30.5951
      },
      "weather": [
        {
          "id": 804,
          "main": "Clouds",
          "description": "阴,多云",
          "icon": "04d"
        }
      ],
      "base": "stations",
      "main": {
        "temp": 281.17,
        "feels_like": 278.08,
        "temp_min": 281.17,
        "temp_max": 281.17,
        "pressure": 1016,
        "humidity": 79,
        "sea_level": 1016,
        "grnd_level": 1012
      },
      "visibility": 10000,
      "wind": {
        "speed": 5.45,
        "deg": 8,
        "gust": 9.08
      },
      "clouds": {
        "all": 100
      },
      "dt": 1676186605,
      "sys": {
        "type": 1,
        "id": 9634,
        "country": "CN",
        "sunrise": 1676156806,
        "sunset": 1676196449
      },
      "timezone": 28800,
      "id": 1806421,
      "name": "Jiang’an Qu",
      "cod": 200
    }
  }
}

接口二:根据城市/地区经纬度查询

{
  "result": 1,
  "description": "TRUE",
  "flag": "NULL",
  "data": {
    "coord": {
      "lon": 114.2999,
      "lat": 30.5951
    },
    "weather": [
      {
        "id": 804,
        "main": "Clouds",
        "description": "阴,多云",
        "icon": "04d"
      }
    ],
    "base": "stations",
    "main": {
      "temp": 281.17,
      "feels_like": 278.08,
      "temp_min": 281.17,
      "temp_max": 281.17,
      "pressure": 1016,
      "humidity": 79,
      "sea_level": 1016,
      "grnd_level": 1012
    },
    "visibility": 10000,
    "wind": {
      "speed": 5.45,
      "deg": 8,
      "gust": 9.08
    },
    "clouds": {
      "all": 100
    },
    "dt": 1676186605,
    "sys": {
      "type": 1,
      "id": 9634,
      "country": "CN",
      "sunrise": 1676156806,
      "sunset": 1676196449
    },
    "timezone": 28800,
    "id": 1806421,
    "name": "Jiang’an Qu",
    "cod": 200
  }
}

扩展接口:根据城市/地区名称查询对应经纬度

{
  "result": 1,
  "description": "TRUE",
  "flag": "NULL",
  "data": {
    "name": "Wuhan",
    "lat": 30.5951051,
    "lon": 114.2999353,
    "info": {
      "local_names": {
        "kn": "ವುಹಾನ್",
        "fj": "Wuhan",
        "kg": "Wuhan",
        "fr": "Wuhan",
        "no": "Wuhan",
        "vi": "Vũ Hán",
        "mk": "Вухан",
        "oc": "Wuhan",
        "ta": "வுகான்",
        "ki": "Wuhan",
        "sr": "Вухан",
        "hr": "Wuhan",
        "am": "ዉሃን",
        "ko": "우한시",
        "ascii": "Wuhan",
        "fo": "Wuhan",
        "da": "Wuhan",
        "ky": "Ухань",
        "it": "Wuhan",
        "id": "Wuhan",
        "eo": "Vuhano",
        "gv": "Wuhan",
        "kl": "Wuhan",
        "my": "ဝူဟန့်မြို့",
        "th": "อู่ฮั่น",
        "lv": "Uhaņa",
        "mn": "Үхань",
        "cs": "Wu-chan",
        "bm": "Wuhan",
        "os": "Ухань",
        "ru": "Ухань",
        "la": "Wuhan",
        "zh": "武汉市",
        "eu": "Wuhan",
        "kk": "Ухань",
        "sq": "Vuhani",
        "pl": "Wuhan",
        "ba": "Ухань",
        "ny": "Wuhan",
        "fa": "ووهان",
        "sk": "Wu-chan",
        "el": "Ουχάν",
        "lb": "Wuhan",
        "ku": "Wuhan",
        "br": "Wuhan",
        "hy": "Ուհան",
        "ce": "Ухань",
        "ar": "ووهان",
        "ca": "Wuhan",
        "mi": "Wuhan",
        "kw": "Wuhan",
        "af": "Wuhan",
        "sh": "Wuhan",
        "tn": "Wuhan",
        "az": "Uhan",
        "za": "Vujhan",
        "be": "Ухань",
        "bn": "উহান",
        "ka": "უხანი",
        "tl": "Wuhan",
        "ja": "武漢市",
        "hi": "वूहान",
        "ga": "Wuhan",
        "nl": "Wuhan",
        "is": "Wuhan",
        "lt": "Uhanas",
        "uz": "Uxan",
        "fi": "Wuhan",
        "ug": "ۋۇخەن شەھىرى",
        "bg": "Ухан",
        "mg": "Wuhan",
        "sw": "Wuhan",
        "wo": "Wuhan",
        "ms": "Wuhan",
        "sv": "Wuhan",
        "ha": "Wuhan",
        "uk": "Ухань",
        "he": "ווהאן",
        "mr": "वूहान",
        "pt": "Wuhan",
        "nn": "Wuhan",
        "en": "Wuhan",
        "gl": "Wuhan",
        "tt": "Үһән",
        "es": "Wuhan",
        "ml": "വൂഹാൻ",
        "ro": "Wuhan",
        "qu": "Wuhan",
        "tr": "Vuhan",
        "ur": "ووہان",
        "hu": "Vuhan",
        "pa": "ਵੂਖ਼ਨ",
        "et": "Wuhan",
        "de": "Wuhan",
        "bo": "ཝུའུ་ཧན་གྲོང་ཁྱེར།",
        "cy": "Wuhan",
        "feature_name": "Wuhan"
      },
      "country": "CN",
      "state": "Hubei"
    }
  }
}

参数说明

参数 必选 类型 描述
result string 接口响应结果:0-失败;1-成功
description string 接口响应描述:一般为 TURE(result=1) 与 FALSE(result=0),或者返回错误信息
flag string 错误说明,没有错误则返回空
data object 返回数据

接口一:根据城市/地区名称查询

data 参数说明:

参数 必选 类型 描述
cityInfo object 城市/地区信息
weather object 天气信息

cityInfo 参数说明:

参数 必选 类型 描述
name string 城市/地区名称
lon string 城市/地区经度
lat string 城市/地区纬度
info object 城市/地区其他名称信息
country string 城市/地区所属国家
state string 城市/地区所属地区

weather 参数说明:

参数 必选 类型 描述
coord object 城市/地区经纬度信息
weather object 天气信息
base string 内部参数
main object 温度湿度及气压信息
visibility string 能见度
dt string 预报时间
sys object 国际级日出日落时间信息
timezone string 从UTC以秒为单位转换
id string 城市/地区ID
name string 城市/地区名称
cod string 内部参数

接口二:根据城市/地区经纬度查询

data 参数说明:

参数 必选 类型 描述
coord object 城市/地区经纬度信息
weather object 天气信息
base string 内部参数
main object 温度湿度及气压信息
visibility string 能见度
dt string 预报时间
sys object 国际级日出日落时间信息
timezone string 从UTC以秒为单位转换
id string 城市/地区ID
name string 城市/地区名称
cod string 内部参数

扩展接口:根据城市/地区名称查询对应经纬度

data 参数说明:

参数 必选 类型 描述
name string 城市/地区名称
lon string 城市/地区经度
lat string 城市/地区纬度
info object 城市/地区其他名称信息
country string 城市/地区所属国家
state string 城市/地区所属地区

SDK 及代码示例

注:以下仅提供接口一的代码示例,接口二及扩展接口请参考接口一的代码示例。

PHP SDK

方法一:以 POST 方式请求数据

//接口参数
$api_url='http://cha.ebaitian.cn/api/json';
$api_appid='1000xxxx';
$api_appkey='56cf61af4b7897e704f67deb88ae8f24';

//函数,以POST方式提交数据,PHP需要开启CURL函数;数据传输安全,建议使用
function getCityWeatherInfo($city,$lang){
    global $api_url,$api_appid,$api_appkey;
    $posturl=$api_url;
    $data='appid='.$api_appid.'&module=getCityWeatherInfo&city='.$city.'&lang='.$lang;
    $sign=hash("sha256",$data.'&appkey='.$api_appkey);
    $postdata=array("appid"=>$api_appid,"appkey"=>$api_appkey,"module"=>"getCityWeatherInfo","city"=>$city,"lang"=>$lang,'sign'=>$sign);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $posturl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
    $output = curl_exec($curl);
    curl_close($curl);
    $obj=json_decode($output);
    $result=$obj->result;
    if($result==1){
        $value=$obj->weatherInfo;
    }else{
        $value=$obj->flag;
    }
    return $value;
}
//调用函数
$city='武汉';
echo getCityWeatherInfo($city,'zh');
exit;

方法二:以 GET 方式请求数据

//接口参数
$api_url='http://cha.ebaitian.cn/api/json';
$api_appid='1000xxxx';
$api_appkey='56cf61af4b7897e704f67deb88ae8f24';

//函数,以GET方式提交数据
function getCityWeatherInfo($city,$lang){
    global $api_url,$api_appid,$api_appkey;
    $data='appid='.$api_appid.'&module=getCityWeatherInfo&city='.$city.'&lang='.$lang;
    $sign=hash("sha256",$data.'&appkey='.$api_appkey);
    $info_get=file_get_contents($api_url.'?type=get&'.$data.'&sign='.$sign);
    $info_json=json_decode($info_get, true);
    $result=$info_json['result'];
    if($result==1){
        $value=$info_json['weatherInfo'];
    }else{
        $value=$info_json['flag'];
    }
    return $value;
}
//调用函数
$city='武汉';
echo getCityWeatherInfo($city,'zh');
exit;

Java SDK

//以下示例是以 GET 方式请求数据
public class QueryHelper {
	
    public static String apiurl="http://cha.ebaitian.cn/api/json";
    public static String appid="1000xxxx";
    public static String appkey="56cf61af4b7897e704f67deb88ae8f24";
    public static String module="getCityWeatherInfo";
    
    public static String getSHA256Str(String str){
        MessageDigest messageDigest;
        String encdeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(str.getBytes("UTF-8"));
            encdeStr = Hex.encodeHexString(hash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encdeStr;
    }
    
    public static String get(String urlString) {
        try {
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5 * 1000);
            conn.setReadTimeout(5 * 1000);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setInstanceFollowRedirects(false);
            conn.setRequestMethod("GET"); 
            int responseCode = conn.getResponseCode();
            if (responseCode == 200) {
                StringBuilder builder = new StringBuilder();
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(conn.getInputStream(),"utf-8"));
                for (String s = br.readLine(); s != null; s = br.readLine()) {
                    builder.append(s);
                }
                br.close();
                return builder.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static String queryWeather(String city, String lang){
        String sign=getSHA256Str("appid="+appid+"&module="+module+"&city="+city+"&lang="+lang+"&appkey="+appkey);
        String url=apiurl+"?type=get&appid="+appid+"&module="+module+"&city="+city+"&lang="+lang+"&sign="+sign;
        return get(url);
    }
}

//使用示例
QueryHelper.queryWeather("武汉",'zh');

Python SDK

#!/usr/bin/python
# -*- coding: utf-8 -*-
import httplib2
import hashlib
from urllib.parse import urlencode #python3
#from urllib import urlencode #python2

apiurl='http://cha.ebaitian.cn/api/json'
appid='1000xxxx'
appkey='56cf61af4b7897e704f67deb88ae8f24'
module='getCityWeatherInfo'
city='武汉'
lang='zh'
data='appid='+appid+'&module='+module+'&city='+city+'&lang='+lang
sign_data=data+'&appkey='+appkey

# from Crypto.Cipher import AES
# from Crypto.Hash import SHA256
# 256
hash_256 = hashlib.sha256()
hash_256.update(sign_data.encode('utf-8'))
sign = hash_256.hexdigest()

postdata = urlencode({'appid':appid,'module':module,'city':city,'lang':lang,'sign':sign})
url = apiurl+'?type=get&'+postdata
http = httplib2.Http()
response, content = http.request(url,'GET')
print(content.decode("utf-8"))

Node.js SDK

方法一:以 POST 方式请求数据

//以 POST 方式提交
var http = require('http');  

var querystring = require('querystring');  

//参数设置
var appid = '1000xxxx';
var appkey = '56cf61af4b7897e704f67deb88ae8f24';
var module = 'getCityWeatherInfo';

//目标查询城市/地区名称
var city='武汉';
var lang='zh';

//签名,SHA256 不可直接调用;函数参考下载地址:https://github.com/alexweber/jquery.sha256
var sign = SHA256('appid='+appid+'&module='+module+'&city='+city+'&lang='+lang+'&appkey='+appkey);

//这是需要提交的数据
var post_data = {
    appid: appid,  
    module: module,
    city: city,
    lang: lang,
    sign: sign
};  

var content = querystring.stringify(post_data);  

var options = {  
    hostname: 'cha.ebaitian.cn',  
    port: 80,  
    path: '/api/json',  
    method: 'POST',  
    headers: {  
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'  
    }  
};  

var req = http.request(options, function (res) {  
    console.log('STATUS: ' + res.statusCode);  
    console.log('HEADERS: ' + JSON.stringify(res.headers));  
    res.setEncoding('utf8');  
    res.on('data', function (chunk) {  
        console.log('BODY: ' + chunk);  
    //JSON.parse(chunk)
    });  
});  

req.on('error', function (e) {  
    console.log('problem with request: ' + e.message);  
});  

// write data to request body  
req.write(content);  

req.end();  

方法二:以 GET 方式请求数据

//以 GET 方式提交
var http = require('http');  

var querystring = require('querystring');  

//参数设置
var appid = '1000xxxx';
var appkey = '56cf61af4b7897e704f67deb88ae8f24';
var module = 'getCityWeatherInfo';

//目标查询城市/地区名称
var city='武汉';
var lang='zh';

//签名,SHA256 不可直接调用;函数参考下载地址:https://github.com/alexweber/jquery.sha256
var sign = SHA256('appid='+appid+'&module='+module+'&city='+city+'&lang='+lang+'&appkey='+appkey);

//这是需要提交的数据
var data = {
    appid: appid, 
    module: module,
    city: city,
    lang: lang,
    sign: sign
};

var content = querystring.stringify(data);  

var options = {  
    hostname: 'cha.ebaitian.cn',  
    port: 80,  
    path: '/api/json?' + content,  
    method: 'GET'  
};  

var req = http.request(options, function (res) {  
    console.log('STATUS: ' + res.statusCode);  
    console.log('HEADERS: ' + JSON.stringify(res.headers));  
    res.setEncoding('utf8');  
    res.on('data', function (chunk) {  
        console.log('BODY: ' + chunk);
    });  
});  

req.on('error', function (e) {  
    console.log('problem with request: ' + e.message);  
});  

req.end();  

C# SDK

using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using System.Text;

public class getCityWeatherInfo{
    public static string getInfo(string appid, string appkey, string module, string city, string lang){
        string url = string.Format("http://cha.ebaitian.cn/api/json?type=get&appid={0}&module={1}&city={2}&sgin={3}", appid, module, city, lang, sgin);
        using (WebClient client = new WebClient()){
            client.Encoding = Encoding.UTF8;
            return client.DownloadString(url);
        }
    }
}

string weatherInfo = getCityWeatherInfo.getInfo("1000xxxx", "getCityWeatherInfo", "北京", "zh", "ecab4881ee80ad3d76bb1da68387428ca752eb885e52621a3129dcf4d9bc4fd4", Request.UserHostAddress);
Console.WriteLine(weatherInfo);
Response.Write(weatherInfo);

JavaScript SDK

方法一:以 POST 方式请求数据

//使用 JQuery 请先加载最新的 JQuery 插件
//参数设置
var apiurl = 'http://cha.ebaitian.cn/api/json';
var appid = '1000xxxx';
var appkey = '56cf61af4b7897e704f67deb88ae8f24';
var module = 'getCityWeatherInfo';

//目标查询城市/地区名称
var city='武汉';
var lang='zh';

//签名,SHA256 不可直接调用;函数参考下载地址:https://github.com/alexweber/jquery.sha256
var sign = SHA256('appid='+appid+'&module='+module+'&city='+city+'&lang='+lang+'&appkey='+appkey);

//提交数据
$.ajax({
    url:apiurl,
    type:'post',
    dataType:'json',
    data:{
        appid:appid,
        module:module,
        city:city,
        lang:lang,
        sign:sign
    },
    success:function(res){
        console.log(res);
    }
});

方法二:以 GET 方式请求数据

//使用 JQuery 请先加载最新的 JQuery 插件
//参数设置
var apiurl = 'http://cha.ebaitian.cn/api/json';
var appid = '1000xxxx';
var appkey = '56cf61af4b7897e704f67deb88ae8f24';
var module = 'getCityWeatherInfo';

//目标查询城市/地区名称
var city='武汉';
var lang='zh';

//签名,SHA256 不可直接调用;函数参考下载地址:https://github.com/alexweber/jquery.sha256
var sign = SHA256('appid='+appid+'&module='+module+'&city='+city+'&lang='+lang+'&appkey='+appkey);

//提交数据
$.ajax({
    url:apiurl,
    type:'post',
    dataType:'json',
    data:{
        appid:appid,
        module:module,
        city:city,
        lang:lang,
        sign:sign
    },
    success:function(res){
        console.log(res);
    }
});

ASP SDK

'设置参数
dim apiurl, appid, appkey, module, city, lang, sign
apiurl="http://cha.ebaitian.cn/api/json"
appid="1000xxxx'
appkey="56cf61af4b7897e704f67deb88ae8f24"
module="getCityWeatherInfo"
city="武汉"
lang="zh"

'签名,SHA256 不可直接调用;函数参考地址:https://blog.csdn.net/yesoce/article/details/128546
sgin=SHA256("appid=&appid&"&module="&module&"&city="&city&"&lang="&lang&"&appkey="&appkey)

'异步提交数据
function PostHTTPPage(url,data)
    dim Http 
    set Http=server.createobject("MSXML2.SERVERXMLHTTP.3.0")
    Http.open "POST",url,false
    Http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    Http.send(data) 
    if Http.readystate<>4 then 
    exit function 
    End if
    PostHTTPPage=bytesToBSTR(Http.responseBody,"UTF-8")
    set http=nothing 
    if err.number<>0 then err.Clear 
End function

'提交数据
dim postdata, strTest
postdata="appid=&appid&"&module="&module&"&city="&city&"&lang="&lang&"&sign="&sign
strTest=PostHTTPPage(apiurl,postdata)

'返回结果
response.write(strTest)
response.end

常见问题

API 接口参数为空

此错误返回 JSON 数据如下:

{
    "result":0,
    "description":"API接口参数为空",
    "flag":"appid:sign"
}

解决方法:
1)请检查 appid 及 sign 是否为空;
2)确保 appid 是从官网获取到正确的接口授权;
3)确保 sign 计算生成是正确的。

API 接口参数无效

此错误返回 JSON 数据如下:

{
    "result":0,
    "description":"API接口参数无效",
    "flag":"appid"
}

解决方法:
1)请检查 appid 是否正确;
2)确保 appid 是从官网获取到正确的接口授权。

API 接口授权已到期

此错误返回 JSON 数据如下:

{
    "result":0,
    "description":"API接口授权已到期",
    "flag":"end:2018-12-31 23:59:59"
}

解决方法:
1)请检查 appid 对应接口授权的期限是否过期;
2)如果接口授权过期,请到官网更新(免费用户直接更新,无需续费)或续费(针对商业付费用户)。

签名错误

此错误返回 JSON 数据如下:

{
    "result":0,
    "description":"签名错误",
    "flag":"getCityWeatherInfo->sign"
}

解决方法:
1)请检查 sign 签名计算是否正确;
2)签名 sign 根据公式 $sign=sha256(appid=$appid&module=getCityWeatherInfo&city=$city&lang=$lang&appkey=$appkey) 生成;其中:appkey 为授权接口的 AppKey,请填写您在我要查官网上申请到的 AppKey 。

请求受限

此错误返回 JSON 数据如下:

{
    "result":0,
    "description":"请求受限",
    "flag":"getCityWeatherInfo->daylimit"
}

解决方法:
1)授权接口已超出当前接口产品请求的最大限制;
2)请根据实际使用需求升级您的接口产品。

其他问题

如果您遇到其他的问题,请用以下方式联系我们,我们将为您提供 API 技术对接帮助。

请直接添加 QQ 技术交流群寻求我们帮助。
我要查API技术对接群①:
群号:821884327
扫码加入我要查API技术对接群①: