如何做一个api接口?
1096
2022-10-25
本文目录一览:
百度API Key申请地址:
创建应用 如图:
提交后得到API Key ,运行结果如下:
扩展资料:
API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。
API函数包含在Windows系统目录下的动态连接库文件中。Windows API是一套用来控制Windows的各个部件的外观和行为的预先定义的Windows函数。
参考资料:百度百科-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("".$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] =
[nightPictureUrl] =
[weather] = 晴
[wind] = 西南风3-4级
[temperature] = 13℃
)
[1] = Array
(
[date]= 周二
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 多云转阴
[wind]= 东北风3-4级
[temperature] = 24 ~ 9℃
)
[2] = Array
(
[date]= 周三
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 中雨转小雨
[wind]= 东北风3-4级
[temperature] = 15 ~ 8℃
)
[3] = Array
(
[date]= 周四
[dayPictureUrl] =
[nightPictureUrl] =
[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] =
[nightPictureUrl] =
[weather] = 晴
[wind] = 西南风3-4级
[temperature] = 13℃
)
[1] = Array
(
[date]= 周二
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 多云转阴
[wind]= 东北风3-4级
[temperature] = 24 ~ 9℃
)
[2] = Array
(
[date]= 周三
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 中雨转小雨
[wind]= 东北风3-4级
[temperature] = 15 ~ 8℃
)
[3] = Array
(
[date]= 周四
[dayPictureUrl] =
[nightPictureUrl] =
[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("".$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("".$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、返回的内容如下:
据我所知,气象局提供三个天气查询接口,如下:
1、实时天气接口
地址: sk/【城市代码】.html
例如:查询北京实时天气:
结果:
{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"13","WD":"南风","WS":"2","SD":"59%","WSE":"2","time":"10:30","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}}
2、今日天气接口
地址:【城市代码】.html
例如:查询北京今日天气:
结果:
{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"15","temp2":"5℃","weather":"阵雨转阴","img1":"d3.gif","img2":"n2.gif","ptime":"08:00"}}
3、今日及未来天气接口【内容最详细】
地址:【城市代码】.html
例如:查询北京今日及未来天气:
结果:
{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2013年10月22日","date":"",
"week":"星期二","fchh":"08","cityid":"101010100","temp1":"15℃~5℃","temp2":"15℃~4℃……
…【省略】"index_tr":"适宜","index_co":"较舒适",
"st1":"11","st2":"1","st3":"13","st4":"5","st5":"14","st6":"4","index_cl":"较不宜",
"index_ls":"不太适宜","index_ag":"不易发"}}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~