网站天气api接口源码(天气预报接口api)

网友投稿 343 2022-12-28

本篇文章给大家谈谈网站天气api接口源码,以及天气预报接口api对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。 今天给各位分享网站天气api接口源码的知识,其中也会对天气预报接口api进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何使用百度天气预报API接口

百度API Key申请地址:http://lbsyun.baidu.com/apiconsole/key

创建应用 如图:

提交后得到API Key 网站天气api接口源码,运行结果如下:

扩展资料:

       API(Application Programming Interface,应用程序编程接口)是一些预先定义网站天气api接口源码的函数网站天气api接口源码,目网站天气api接口源码的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力网站天气api接口源码,而又无需访问源码,或理解内部工作机制的细节。 

API函数包含在Windows系统目录下的动态连接库文件中。Windows API是一套用来控制Windows的各个部件的外观和行为的预先定义的Windows函数。

参考资料:百度百科-api接口

如何使用PHP调用API接口实现天气查询功能

最近在做微信公众平台测试时,想在里面子菜单上添加查询未来几天(包括今天)天气的功能,就查找了下好用的天气预报查询接口API,使用比较多的有:国家气象局天气接口、新浪天气预报接口、百度天气预报接口、google天气接口、Yahoo天气接口等等,我使用的是百度提供的免费天气查询接口API,下面与大家分享下...

1、查询方式:

百度提供的是根据纬度和城市名查询天气情况

2、接口事例:

3、接口参数说明:


4、返回结果说明:


5、

//城市名

$city = '上海';

//对json格式的字符串进行编码

$arr =json_decode($str,TRUE);

print_r($atr);

//城市名
   $city = '上海';
 
   //获取json格式的数据
   $str =file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=".$city."output=jsonak=5slgyqGDENN7Sy7pw29IUvrZ");

   //对json格式的字符串进行编码
   $arr =json_decode($str,TRUE);      

   print_r($atr);

6、返回页面的是json编码后的数据:

[plain] view plain copy print?

<meta charset="UTF-8"

Array

(

[error] = 0

[status] = success

[date] = 2014-03-17

[results] = Array

(

[0] = Array

(

[currentCity]= 上海

[weather_data]= Array

(

[0]= Array

(

[date] = 周一(今天, 实时:19℃)

[dayPictureUrl] =http://api.map.baidu.com/images/weather/day/qing.png

[nightPictureUrl] =http://api.map.baidu.com/images/weather/night/qing.png

[weather] = 晴

[wind] = 西南风3-4级

[temperature] = 13℃

)

[1] = Array

(

[date]= 周二

[dayPictureUrl] =http://api.map.baidu.com/images/weather/day/duoyun.png

[nightPictureUrl] = http://api.map.baidu.com/images/weather/night/yin.png

[weather]= 多云转阴

[wind]= 东北风3-4级

[temperature] = 24 ~ 9℃

)

[2] = Array

(

[date]= 周三

[dayPictureUrl] =http://api.map.baidu.com/images/weather/day/zhongyu.png

[nightPictureUrl] = http://api.map.baidu.com/images/weather/night/xiaoyu.png

[weather]= 中雨转小雨

[wind]= 东北风3-4级

[temperature] = 15 ~ 8℃

)

[3] = Array

(

[date]= 周四

[dayPictureUrl] =http://api.map.baidu.com/images/weather/day/duoyun.png

[nightPictureUrl] =http://api.map.baidu.com/images/weather/night/qing.png

[weather]= 多云转晴

[wind]= 北风3-4级

[temperature] = 14 ~ 6℃

)

)

)

)

)

<meta charset="UTF-8"
Array
(
   [error] = 0
   [status] = success
   [date] = 2014-03-17
   [results] = Array
       (
           [0] = Array
               (
                   [currentCity]= 上海
                   [weather_data]= Array
                       (
                           [0]= Array
                               (
                                  [date] = 周一(今天, 实时:19℃)
                                  [dayPictureUrl] =http://api.map.baidu.com/images/weather/day/qing.png
                                  [nightPictureUrl] =http://api.map.baidu.com/images/weather/night/qing.png
                                   [weather] = 晴
                                  [wind] = 西南风3-4级
                                  [temperature] = 13℃
                               )

                           [1] = Array
                               (
                                   [date]= 周二
                                  [dayPictureUrl] =http://api.map.baidu.com/images/weather/day/duoyun.png
                                  [nightPictureUrl] = http://api.map.baidu.com/images/weather/night/yin.png
                                   [weather]= 多云转阴
                                   [wind]= 东北风3-4级
                                  [temperature] = 24 ~ 9℃
                               )

                           [2] = Array
                               (
                                   [date]= 周三
                                  [dayPictureUrl] =http://api.map.baidu.com/images/weather/day/zhongyu.png
                                  [nightPictureUrl] = http://api.map.baidu.com/images/weather/night/xiaoyu.png
                                   [weather]= 中雨转小雨
                                   [wind]= 东北风3-4级
                                  [temperature] = 15 ~ 8℃
                               )

                           [3] = Array
                               (
                                   [date]= 周四
                                  [dayPictureUrl] =http://api.map.baidu.com/images/weather/day/duoyun.png
                                   [nightPictureUrl] =http://api.map.baidu.com/images/weather/night/qing.png
                                   [weather]= 多云转晴
                                   [wind]= 北风3-4级
                                  [temperature] = 14 ~ 6℃
                               )

                       )

               )

       )

)

7、PHP中自带了处理json格式字符串的内置函数,下面做一个事例,并给出完整代码:

[php] view plain copy print?

<metacharset="UTF-8"

<?php

//城市名

$city = '上海';

//获取json格式的数据

$str = file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=".$city."output=jsonak=5slgyqGDENN7Sy7pw29IUvrZ");

//对json格式的字符串进行编码

$arr = json_decode($str,TRUE);

echo "城市:".$arr['results'][0]['currentCity']." 日期:".$arr['date']."<br /<br /";

foreach($arr['results'][0]['weather_data']as $val)

{

echo $val['date']."<br/";

echo "天气:{$val['weather']}<br/";

echo "风向:{$val['wind']}<br/";

echo "温度:{$val['temperature']}<br/<br /";

}

?

<metacharset="UTF-8"
<?php
   //城市名
   $city = '上海';
 
   //获取json格式的数据
   $str = file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=".$city."output=jsonak=5slgyqGDENN7Sy7pw29IUvrZ");

   //对json格式的字符串进行编码
   $arr = json_decode($str,TRUE);      

   echo "城市:".$arr['results'][0]['currentCity']." 日期:".$arr['date']."<br /<br /";

   foreach($arr['results'][0]['weather_data']as $val)
   {
       echo $val['date']."<br/";
       echo "天气:{$val['weather']}<br/";
       echo "风向:{$val['wind']}<br/";
       echo "温度:{$val['temperature']}<br/<br /";
   }
?

8、返回的内容如下:


天气预报API接口是什么?

天气预报API接口是气象服务商为客户提供服务的唯一接口。拿墨迹赤必为例网站天气api接口源码,跟客户充分沟通后了解客户的实际需求网站天气api接口源码,从而对现有的数据进行调整网站天气api接口源码,符合客户的需求形成API接口网站天气api接口源码,让客户接入,这样就形成了一个完整的服务。所以说API接口并不是一个很神秘的东西,只不过是企业和客户之间的一条服务纽带。

有哪些免费开放且收录城市较完整的天气 API 接口

一 天气预报
国内的天气预报数据,主要是国家气象局和国外两个来源(指数据来源)。
国外方面主要是雅虎(XML格式,谷歌已经停掉了自家的天气API);
国内数据由国家气象局提供(JSON格式)。
在知乎看到过一篇专业人士评论,雅虎可以提供全球范围的且非常精细的天气数据(小时级更新),但主要是通过计算机自动生成。而国家气象局则是各地经验丰富的专家人士结合计算机综合分析得出,在准确性上更胜一筹。我就这样信了!
国家气象局接口资源及用法
气象局的天气预报主页:移动版普通版 ,仔细挖掘这两个地址可以发现很多好东西哦。
特点:接口多,来自http://weather.com.cn和http://mobile.weather.com.cn都有,次数不限制,服务稳定,功能全面,优先推荐。
1. 实时天气接口
有多个接口,好像差别不大:
实时天气1:http://www.weather.com.cn/data/sk/101190408.html
实时天气2:http://www.weather.com.cn/data/cityinfo/101190408.html
实时天气3(带时间戳):http://mobile.weather.com.cn/data/sk/101010100.html?_=1381891661455
2. 一周天气预报接口
7天预报数据 URL: http://mobile.weather.com.cn/data/forecast/101010100.html?_=1381891660081
该接口来源气象局移动版网站,目测数据最为精确,详细接口格式如下:
{"c":{"c1":"101010100","c2":"beijing","c3":"北京","c4":"beijing","c5":"北京","c6":"beijing","c7":"北京","c8":"china","c9":"中国","c10":"1","c11":"010","c12":"100000","c13":"116.391","c14":"39.904","c15":"33","c16":"AZ9010","c17":"+8"},
"f":{"f1":
[
{"fa":"01","fb":"03","fc":"10","fd":"5","fe":"0","ff":"0","fg":"0","fh":"0","fi":"06:21|17:40"},
{"fa":"07","fb":"07","fc":"19","fd":"12","fe":"0","ff":"0","fg":"0","fh":"0","fi":"06:22|17:38"},
{"fa":"02","fb":"00","fc":"15","fd":"5","fe":"8","ff":"8","fg":"3","fh":"1","fi":"06:23|17:37"},
{"fa":"00","fb":"00","fc":"16","fd":"4","fe":"0","ff":"0","fg":"0","fh":"0","fi":"06:24|17:35"},
{"fa":"00","fb":"00","fc":"18","fd":"7","fe":"0","ff":"0","fg":"0","fh":"0","fi":"06:25|17:34"},
{"fa":"00","fb":"01","fc":"18","fd":"8","fe":"0","ff":"0","fg":"0","fh":"0","fi":"06:26|17:32"},
{"fa":"01","fb":"01","fc":"16","fd":"6","fe":"0","ff":"0","fg":"0","fh":"0","fi":"06:27|17:31"}],
"f0":"201310121100"}}
详细接口分析如下:
//格式说明
var format={fa:图片1,fb:图片2,fc:温度1,fd:温度2,fe:风向1,ff:风向2,fg:风力1,fh:风力2,fi:日出日落};
//定义天气类型
var weatherArr={"00":"晴","01":"多云","02":"阴","03":"阵雨","04":"雷阵雨","05":"雷阵雨伴有冰雹","06":"雨夹雪","07":"小雨","08":"中雨","09":"大雨","10":"暴雨","11":"大暴雨","12":"特大暴雨","13":"阵雪","14":"小雪","15":"中雪","16":"大雪","17":"暴雪","18":"雾","19":"冻雨","20":"沙尘暴","21":"小到中雨","22":"中到大雨","23":"大到暴雨","24":"暴雨到大暴雨","25":"大暴雨到特大暴雨","26":"小到中雪","27":"中到大雪","28":"大到暴雪","29":"浮尘","30":"扬沙","31":"强沙尘暴","53":"霾","99":""};
//定义风向数组
var fxArr={"0":"无持续风向","1":"东北风","2":"东风","3":"东南风","4":"南风","5":"西南风","6":"西风","7":"西北风","8":"北风","9":"旋转风"};
//定义风力数组
var flArr={"0":"微风","1":"3-4级","2":"4-5级","3":"5-6级","4":"6-7级","5":"7-8级","6":"8-9级","7":"9-10级","8":"10-11级","9":"11-12级"};
6天预报数据 URL: http://m.weather.com.cn/data/101190408.html
该接口来源网络,应该是一个流传比较久的接口,大发现准确性和稳定性可能不稳定。接口分析如下:
{"weatherinfo":{
#基本信息
"city":"北京","city_en":"beijing","date_y":"2013年9月24日","date":"","week":"星期二","fchh":"11","cityid":"101010100",
#6天温度范围
"temp1":"22℃~11℃","temp2":"25℃~11℃","temp3":"26℃~13℃","temp4":"25℃~15℃","temp5":"25℃~16℃","temp6":"24℃~16℃",
#6天温度范围(华氏)
"tempF1":"71.6℉~51.8℉","tempF2":"77℉~51.8℉","tempF3":"78.8℉~55.4℉","tempF4":"77℉~59℉","tempF5":"77℉~60.8℉","tempF6":"75.2℉~60.8℉",
#6天天气
"weather1":"多云转晴","weather2":"晴","weather3":"晴","weather4":"晴转多云","weather5":"阴转阵雨","weather6":"多云",
#6天天气图示(每天2张)
"img1":"1","img2":"0","img3":"0","img4":"99","img5":"0","img6":"99","img7":"0","img8":"1","img9":"2","img10":"3","img11":"1","img12":"99",
"img_single":"1",
#6天天气图示文字(每天2)
"img_title1":"多","img_title2":"晴","img_title3":"晴","img_title4":"晴","img_title5":"晴","img_title6":"晴","img_title7":"晴","img_title8":"多云","img_title9":"阴","img_title10":"阵雨","img_title11":"多云","img_title12":"多云",
"img_title_single":"多云",
#6天风
"wind1":"北风3-4级转微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风",
"fx1":"北风","fx2":"微风",
#6天风力情况
"fl1":"3-4级转小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级",
#24小时内
"index":"较冷","index_d":"建议着大衣、呢外套加毛衣、卫衣等服装。体弱者宜着厚外套、厚毛衣。因昼夜温差较大,注意增减衣服。",
#48小时内
"index48":"较舒适","index48_d":"建议着薄外套或牛仔衫裤等服装。年老体弱者宜着夹克衫、薄毛衣等。昼夜温差较大,注意适当增减衣服。",
#紫外 洗车 旅游 舒适指数 晨练 晾晒 过敏
"index_uv":"弱",
"index48_uv":"强",
"index_xc":"适宜",
"index_tr":"适宜",
"index_co":"舒",
"st1":"21","st2":"12","st3":"24","st4":"12","st5":"25","st6":"14", #不明
"index_cl":"较适宜",
"index_ls":"适宜",
"index_ag":"极易发"
}}

如何使用中国气象局API开发天气APP

前期的准备工作:
一、申请API(拿好appid和private_key)

二、解读《SmartWeatherAPI<Lite WebAPI版接口使用说明书》

三、准备好areaid、type、date、appid、urlencode($key)(注意,这里经加密的key是需要encodeurl之后的才能成为接口链接的一部分)
好了下面的编码开始:
1、从附件中的areaid_list中找到你想要的地方的areaid,并且选择要查询天气的类型
NSString *areaid = @"101010100";
NSString *type =
@"index_f";
/**
* 官方文档更新的数据类型号
* 指数:index_f(基础接口);index_v(常规接口)
3天预报:forecast_f(基础接口);forecast_v(常规接口)
*
*/
2、获得当前天气date
NSDate
*_date = [NSDate date];
NSDateFormatter *dateFormatter =
[[NSDateFormatter alloc] init];
[dateFormatter
setDateFormat:@"yyyyMMddHHmmss"];//注意日期的格式
NSString *date =
[[dateFormatter stringFromDate:_date]
substringToIndex:12];//用到的精确到分,24小时制60分钟制
3、申请的appid,和private_key
NSString *appid =
@"15ds45s13a465s";//这里是楼主随便输入的,瞎编的
NSString *private_key =
@"46s4ds_SmartWeatherAPI_45s44d6";//也是瞎编的
4、算出经过urlencode后的key,这步比较重要,步骤也多,请耐心看完。
在原来的的基础上是在PHP的环境中算出的,代码如下,可在“
http://writecodeonline.com/php/ ”下进行算法的检验
echo
urlencode(base64_encode(hash_hmac('sha1', " http://open.weather.com.cn/data/?areaid=101010100type=index_fdate=201409041509appid=15ds45s13a465s",
"46s4ds_SmartWeatherAPI_45s44d6",
TRUE)));
首先定义得到public_key和API的方法,还有就是对key进行encodeurl操作的方法
注意,这里的方法都是被我定义在getTime的类里面,后面是在main中实例化出来的
//获得publicky
- (NSString*)
getPublicKey:(NSString*)areaid :(NSString*)type :(NSString*)date
:(NSString*)appid {

NSString *Key = [[NSString alloc]
initWithFormat:@"http://open.weather.com.cn/data/?areaid=%@type=%@date=%@appid=%@",
areaid, type, [date substringToIndex:12], appid];

return
Key;
}
//获得完整的API
- (NSString*) getAPI:(NSString*)areaid
:(NSString*)type :(NSString*)date :(NSString*)appid :(NSString*)key
{

NSString *API = [[NSString alloc]
initWithFormat:@"http://open.weather.com.cn/data/?areaid=%@type=%@date=%@appid=%@key=%@",
areaid, type, [date substringToIndex:12], [appid substringToIndex:6],
key];
//-------------这里需要主要的是只需要appid的前6位!!!

return
API;

}
//将获得的key进性urlencode操作
- (NSString
*)stringByEncodingURLFormat:(NSString*)_key{
NSString *encodedString
= (__bridge NSString
*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)_key,
nil, (CFStringRef) @"!$'()*+,-./:;=?@_~%#[]",
kCFStringEncodingUTF8);
//由于ARC的存在,这里的转换需要添加__bridge,原因我不明。求大神讲解
return
encodedString;

}
重点来了,在oc下的算法如下,记得把附件的Base64.h
加进来并引用到工程里面
//对publickey和privatekey进行加密
- (NSString *)
hmacSha1:(NSString*)public_key :(NSString*)private_key{

NSData*
secretData = [private_key
dataUsingEncoding:NSUTF8StringEncoding];
NSData* stringData = [public_key
dataUsingEncoding:NSUTF8StringEncoding];

const void* keyBytes =
[secretData bytes];
const void* dataBytes = [stringData
bytes];

///#define CC_SHA1_DIGEST_LENGTH 20 /* digest
length in bytes */
void* outs =
malloc(CC_SHA1_DIGEST_LENGTH);

CCHmac(kCCHmacAlgSHA1, keyBytes,
[secretData length], dataBytes, [stringData length], outs);

//
Soluion 1
NSData* signatureData = [NSData dataWithBytesNoCopy:outs
length:CC_SHA1_DIGEST_LENGTH freeWhenDone:YES];

return
[signatureData
base64EncodedString];

}
这里只是初步算出来的key,还未encodeurl,链接不能被浏览器识别,所以现在经过算法得到的_key还有一步操作才能的到真正的key。
NSString *_key = [getTime hmacSha1:[getTime
getPublicKey:areaid :type :date :appid] :private_key];
NSString *key =
[getTime
stringByEncodingURLFormat:_key];
最后一步了吧!拼接API
NSString *weatherAPI = [getTime getAPI:areaid :type :date
:appid
:key];
//OK,我们的API就可以用啦。
最后,通过API返回的值是JSON文件,通过解析,就能得到我们想要的数据了,下面拿一个开发的接口举例
NSDictionary *weatherDic = [getTime
getWeatherDic:@"http://www.weather.com.cn/data/cityinfo/101010100.html"];

// weatherDic字典中存放的数据也是字典型,从它里面通过键值取值
NSDictionary
*weatherInfo = [weatherDic
objectForKey:@"weatherinfo"];
NSLog(@"今天是 %@ %@ %@ 的天气状况是:%@ %@ -
%@",[newDateOne substringWithRange:NSMakeRange(0, 4)],[newDateOne
substringWithRange:NSMakeRange(4, 2)] ,[newDateOne
substringWithRange:NSMakeRange(6, 2)],[weatherInfo
objectForKey:@"weather"],[weatherInfo objectForKey:@"temp1"],[weatherInfo
objectForKey:@"temp2"]);
输出:2014-09-04 23:40:23.243
WeatherAPP[5688:201108] 今天是 2014-09-04 的天气状况是:晴 17℃ - 30℃
weatherInfo字典里面的内容是---{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"17℃","temp2":"30℃","weather":"晴","img1":"n0.gif","img2":"d0.gif","ptime":"18:00"}}

免费天气api接口怎么应用到安卓源代码里

我今天也收到客户抱怨网站天气api接口源码了,上中国天气网查看到了一个公告,看样子是API要变网站天气api接口源码的节奏: 1234为更好地为用户提供气象数据服务,新版气象数据开放平台于2014年11月17日正式上线。新用户可在线注册帐户,详细填写用户资料,按照申请流程提示申请数据接口,审核通过后,系统会自动分发密钥。新用户API数据接口通过审核后一年有效,逾期未续约将自动失效,API数据产品类型详见开放平台产品中心。 已申请并开通接口的老用户可继续使用原有API接口,原有API接口自今日起一年内有效,于2015年11月16日自动关闭,如有使用需要,建议老用户在接口关闭之前申请新API接口,申请流程及具体说明参见新用户申请。 请广大开发者相互转告,我们一直努力,为大家提供不断优化的气象数据服务 关于网站天气api接口源码和天气预报接口api的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。 网站天气api接口源码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于天气预报接口api、网站天气api接口源码的信息别忘了在本站进行查找喔。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java面试题冲刺第二天
下一篇:SpringBoot拦截器以及源码详析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~