网络API接口的使用

一:先介绍一下什么是网络API接口
    网络API接口就是各种大公司对外提供的一种各种信息和数据获取的接口像百度,腾讯,阿里巴巴等都提供这种接口,一些小公司和个人就可以通过这些接口获取各种信息,如城市天气信息,生成二维码,地图信息,手机号码归属地,摄像头,卫星定位等。这些小公司就可以通过付费的方式使用这些接口,从而做出像天气预报查询,地图导航等一系列的APP。对于我们学习使用来讲这些接口大部分都有免费使用次数,足够我们学习使用

二:我在这里介绍一下两种API接口
1: k780.com     这个网站提供的接口可免费使用
 进入网页看到下图
 

点击上面的数据接口可以看到许多接口

2: 百度API接口
 可以先注册一个百度账号,百度API同样有免费使用次数,足够学习使用。

使用方法:
 我就以天气预报解析作为示例
 我们从k780.com 获取天气预报接口如下
 http://api.k780.com:88/app=weather.future&weaid=1&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json
 注:
      1:接口中weaid=后面接的就是你想查询的城市名称,比如我所在的城市南昌
      2:由于我们在程序中获取这条接口命令传回来的信息是直接输入在Linux系统
      的命令行上,而&是特殊字符,所以要用单引号引起来
      3:用Linux命令curl来运行这条接口命令 
      4:在命令的结尾处需要将返回的信息存放在文件中,可在结尾将信息定向到一
      个文件中例如:> 123.txt
 综合上述几点注意,可得命令为:
 curl    http://api.k780.com:88/?app=weather.future'&'weaid=南昌'&&'appkey=10003'&'sign=b59bc3ef6191eb9f747dd4e83c99f2a4'&'
 format=json > 123.txt

三:在C语言程序中如何使用:
 可用C语言system函数来执行命令。
 
四:获取的天气信息如下:(格式为json格式)

{"success":"1","result":[{"weaid":"237","days":"2018-02-05","week":"星期一",
"cityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"7℃/
-1℃","humidity":"0%/0%","weather":"晴","weather_icon":"http://api.k780.com/
upload/weather/d/0.gif","weather_icon1":"http://api.k780.com/upload/weather/
n/0.gif","wind":"东北风","winp":"<3级","temp_high":"7","temp_low":"-1","humi
_high":"0","humi_low":"0","weatid":"1","weatid1":"1","windid":"13","winpid":
"395"},{"weaid":"237","days":"2018-02-06","week":"星期二","cityno":"nanchang
","citynm":"南昌","cityid":"101240101","temperature":"8℃/1℃","humidity":"0
%/0%","weather":"晴转多云","weather_icon":"http://api.k780.com/upload/weathe
r/d/0.gif","weather_icon1":"http://api.k780.com/upload/weather/n/1.gif","win
d":"东北风","winp":"<3级","temp_high":"8","temp_low":"1","humi_high":"0","hu
mi_low":"0","weatid":"1","weatid1":"2","windid":"13","winpid":"395"},{"weaid
":"237","days":"2018-02-07","week":"星期三","cityno":"nanchang","citynm":"南
昌","cityid":"101240101","temperature":"10℃/2℃","humidity":"0%/0%","weathe
r":"多云","weather_icon":"http://api.k780.com/upload/weather/d/1.gif","weath
er_icon1":"http://api.k780.com/upload/weather/n/1.gif","wind":"东北风转北风"
,"winp":"<3级","temp_high":"10","temp_low":"2","humi_high":"0","humi_low":"0
","weatid":"2","weatid1":"2","windid":"25","winpid":"395"},{"weaid":"237","d
ays":"2018-02-08","week":"星期四","cityno":"nanchang","citynm":"南昌","cityi
d":"101240101","temperature":"11℃/4℃","humidity":"0%/0%","weather":"多云转
阴","weather_icon":"http://api.k780.com/upload/weather/d/1.gif","weather_icon
1":"http://api.k780.com/upload/weather/n/2.gif","wind":"东北风转北风","winp":
"<3级","temp_high":"11","temp_low":"4","humi_high":"0","humi_low":"0","weatid
":"2","weatid1":"3","windid":"25","winpid":"395"},{"weaid":"237","days":"2018
-02-09","week":"星期五","cityno":"nanchang","citynm":"南昌","cityid":"1012401
01","temperature":"6℃/5℃","humidity":"0%/0%","weather":"小雨","weather_icon
":"http://api.k780.com/upload/weather/d/7.gif","weather_icon1":"http://api.k7
80.com/upload/weather/n/7.gif","wind":"北风","winp":"<3级","temp_high":"6","t
emp_low":"5","humi_high":"0","humi_low":"0","weatid":"8","weatid1":"8","windi
d":"20","winpid":"395"},{"weaid":"237","days":"2018-02-10","week":"星期六","c
ityno":"nanchang","citynm":"南昌","cityid":"101240101","temperature":"10℃/2℃
","humidity":"0%/0%","weather":"阴转多云","weather_icon":"http://api.k780.com/
upload/weather/d/2.gif","weather_icon1":"http://api.k780.com/upload/weather/n/
1.gif","wind":"北风","winp":"<3级","temp_high":"10","temp_low":"2","humi_high"
:"0","humi_low":"0","weatid":"3","weatid1":"2","windid":"20","winpid":"395"},{
"weaid":"237","days":"2018-02-11","week":"星期日","cityno":"nanchang","citynm"
:"南昌","cityid":"101240101","temperature":"10℃/3℃","humidity":"0%/0%","weat
her":"多云转阴","weather_icon":"http://api.k780.com/upload/weather/d/1.gif","w
eather_icon1":"http://api.k780.com/upload/weather/n/2.gif","wind":"北风转无持续
风向","winp":"<3级","temp_high":"10","temp_low":"3","humi_high":"0","humi_low":
"0","weatid":"2","weatid1":"3","windid":"126","winpid":"395"}]}

注:获取到上述信息后,我们所要做的工作就是用C语言代码将信息解析出来

五:下面附上我自己的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define WEEK_INFO_NUM   14
static char *str1="curl http://api.k780.com:88/?app=weather.future'&'weaid=";
static char *str2="'&&'appkey=10003'&'sign=b59bc3ef6191eb9f747dd4e83c99f2a4'&'format=json >wea.txt";
//定义一个保存天气信息的结构体
typedef struct _tag_Weather_Info
{
 char date[11];//日期
 char week[10];//星期
 char addr_pinyin[20];//地址拼音
 char addr_name[20];//地址中文
 char city_id[20];//城市id
 char temperature[10];//温度
 char humidity[10];//湿度
 char weather[20];//气候
 char wind[20];//风向
 char winp[10];//风力
 char temp_high[5];//最高气温
 char temp_low[5];//最低气温
 char humi_high[5];//最高湿度
 char humi_low[5];//最低湿度
}Wea_Info;
Wea_Info weather_info[14];//用于存放7天的天气信息
char *wea_flag[14]={"days","week","cityno","citynm","cityid","temperature",\
"humidity","weather","wind","winp","temp_high","temp_low","humi_high","humi_low"};
char buf[14][20];//定义一个存放信息的缓冲区

/*
 函数名称:move_to_info
 函数功能:将字符串移动到要提取的信息位置
 参数:char *str--字符串
 返回值:char *--移动后的字符串
 */
char *move_to_info(char *str)
{
 while(1)
 {
  if(*str==':')
   break;
  str++;
 }
 str+=2;
 return str;
}

/*
 函数名称:Get_Info
 函数功能:获取天气的单个信息
 参数:char *src--存放天气信息的字符串 int flag--要提取的信息编号 
 返回值:char *--提取完一次的字符串
 */
char *Get_Info(char *src,int flag)
{
 int i=0;
 while(1)
 {
  if(*src=='"')
   break;
  else
  {
   buf[flag][i++]=*src;
   src++;
  }
 }
 buf[flag][i]='\0';
 return src;
}

/*
 函数名称:Get_Weather_Info
 函数功能:获取天气信息
 参数:char *src_info--存放天气信息的字符串
 返回值:char *--提取完一次的字符串
 */
char *Get_Weather_Info(char *src_info,int num)
{
 int i=0;
 char *temp=NULL;
 for(i=0;i<WEEK_INFO_NUM;i++)
 {
  temp=strstr(src_info,wea_flag[i]);
  temp=move_to_info(temp);
  temp=Get_Info(temp,i);
 }
 i=0;
 //将信息放入结构体
 strcpy(weather_info[num].date,buf[i++]);
 strcpy(weather_info[num].week,buf[i++]);
 strcpy(weather_info[num].addr_pinyin,buf[i++]);
 strcpy(weather_info[num].addr_name,buf[i++]);
 strcpy(weather_info[num].city_id,buf[i++]);
 strcpy(weather_info[num].temperature,buf[i++]);
 strcpy(weather_info[num].humidity,buf[i++]);
 strcpy(weather_info[num].weather,buf[i++]);
 strcpy(weather_info[num].wind,buf[i++]);
 strcpy(weather_info[num].winp,buf[i++]);
 strcpy(weather_info[num].temp_high,buf[i++]);
 strcpy(weather_info[num].temp_low,buf[i++]);
 strcpy(weather_info[num].humi_high,buf[i++]);
 strcpy(weather_info[num].humi_low,buf[i++]);
 return temp;
}

/*
 函数名称:Print_Weather_Info
 函数功能:打印天气信息
 参数:int num--天数编号
 返回值:void
 */
void Print_Weather_Info(int num)
{
 printf("日期:%s\n",weather_info[num].date);
 printf("星期:%s\n",weather_info[num].week);
 printf("地址拼音:%s\n",weather_info[num].addr_pinyin);
 printf("地址中文:%s\n",weather_info[num].addr_name);
 printf("城市ID:%s\n",weather_info[num].city_id);
 printf("温度:%s\n",weather_info[num].temperature);
 printf("湿度:%s\n",weather_info[num].humidity);
 printf("气候:%s\n",weather_info[num].weather);
 printf("风向:%s\n",weather_info[num].wind);
 printf("风力:%s\n",weather_info[num].winp);
 printf("最高气温:%s\n",weather_info[num].temp_high);
 printf("最低气温:%s\n",weather_info[num].temp_low);
 printf("最高湿度:%s\n",weather_info[num].humi_high);
 printf("最低湿度:%s\n",weather_info[num].humi_low);
}
int main(int argc,char **argv)
{
 if(argc!=2)
 {
  printf("传参格式:./a.out <城市名>\n");
  exit(-1);
 }

//拼接命令行字符串
 char *ptr=malloc(strlen(str1)+strlen(str2)+strlen(argv[1])+1);
 strcpy(ptr,str1);
 strcat(ptr,argv[1]);
 strcat(ptr,str2);

//获取天气信息并将信息存放到123.txt
 system(ptr);
 FILE *fpr=fopen("wea.txt","rb");
 if(fpr==NULL)
 {
  printf("打开文件失败!\n");
  exit(-1);
 }
 //利用stat获取文件大小
 struct stat stat_buf;
 stat("wea.txt", &stat_buf);
 char *src=malloc(stat_buf.st_size);
 fread(src,1,stat_buf.st_size,fpr);
 fclose(fpr);
 int i=0;
 char *temp=src;
 for(i=0;i<7;i++)
 {
  memset(buf,0,sizeof(buf));
  temp=Get_Weather_Info(temp,i);
 }
 for(i=0;i<7;i++)
 {
  Print_Weather_Info(i);
  printf("\n");
 }
 return 0;
}

六:实际在解析过程中有两种方法可以解析,一种就是上面的自己找规律将信息解析出来,还有一种是通过cjson库来解析数据,这方面的知识可以到网上搜索一下,如果学会了cjson的使用,那解析数据就不用像上面一样那么麻烦。



网络API接口的使用相关推荐

  1. 网络API接口的详细使用

    文章目录 API简介 API接口的申请 API接口的要素: 接入地址 输入参数 请求方式 输出参数 实战应用 正确地获取入参 发送请求 解析出参 完整Java代码: API简介 API(Applica ...

  2. 如何实现高质量的系统数据对接/API接口整合集成方案?

    我们建立一个数字化系统可能会有跟别的系统进行数据对接的需要,而别人的数字化系统也有可能需要跟我们的系统进行数据对接,也可能我们开发了不同的网站,网站小应用之间需要进行数据对接.那么数据对接应该如何操作 ...

  3. 基于Qt的网络音乐播放器(四)酷狗API接口获取歌曲的搜索列表和歌曲的播放

    2020博客之星年度总评选进行中:请为74号的狗子投上宝贵的一票! 我的投票地址:点击为我投票 文章目录 1.效果图 2.准备好前面获取的酷狗api接口 3.网络歌曲搜索实现 4.网络歌曲播放的实现 ...

  4. 网络音乐api接口,歌词api接口,免费

    网络音乐api接口,歌词api接口,免费 具体请看项目地址 项目地址: http://showdoc.dagoogle.cn/index.php/4 网络音乐api开放接口 所有这些都是为了学习,交流 ...

  5. Python零基础速成班-第12讲-Python获取网络数据Socket,API接口,网络爬虫Crawler(制作弹幕词云)

    Python零基础速成班-第12讲-Python获取网络数据Socket,API接口,网络爬虫Crawler(制作弹幕词云) 学习目标 获取网络数据Socket API接口 网络爬虫Crawler(制 ...

  6. 淘宝API接口(网络爬虫数据)

    淘宝API接口(部分) item_get 获得淘宝商品详情 item_get_pro 获得淘宝商品详情高级版 item_review 获得淘宝商品评论 item_fee 获得淘宝商品快递费用 item ...

  7. 拒绝接口裸奔!开放API接口签名验证!

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 来源:r6d.cn/kChH 接口安全问题 请求身份是否合 ...

  8. ​如何设计一个安全可靠的 API 接口?

    作者 | 阿文 责编 | 屠敏 出品 | CSDN(ID:CSDNnews) 最近几年,随着RESTful API开始风靡,使用HTTP header来传递认证令牌似乎变得理所应当,通过 RESTfu ...

  9. 为 UWP 应用提供的 .NET 网络 API

    [编者按]本文作者是 Windows networking 团队项目经理 Sidharth Nabar.在微软 Build 2015 大会上,.NET Core 5 作为开发 UWP(Universa ...

  10. php api接口调试,PHP进行API接口测试

    最近写API接口,每写一个接口,我自己需要先测试一下,看有没有语法错误,请求的数据对不对,但是很多都是POST请求,没法直接在浏览器中打开链接进行测试,所以必须要有个可以在本地发HTTP请求的模拟工具 ...

最新文章

  1. int(1) 和 int(10) 有什么区别?资深开发竟然分不清!
  2. Python之web开发(五):WEB开发html语句经典应用
  3. android 汉字转拼音pinyin4j包
  4. jdbc增删改查有哪些步骤_用Mybatis如何实现对数据库的增删改查步骤
  5. css3制作炫酷导航栏效果 转
  6. 平板电脑有哪些品牌_平板电脑充电柜使用要注意哪些?安和力
  7. TP-Link 开始锁定路由器固件禁止安装开源固件
  8. 论《LEFT JOIN条件放ON和WHERE后的区别》
  9. [BZOJ1492][NOI2007]货币兑换Cash(斜率优化+CDQ分治)
  10. 15.深入分布式缓存:从原理到实践 --- 同程凤凰缓存系统基于Redis的设计与实践
  11. zookeeper 进入客户端_探究—Zookeeper的启动过程
  12. 通过阿里云容器镜像服务下载谷歌gcr.io镜像
  13. ubuntu19 磁盘分区管理及磁盘挂载
  14. ppt背景图片怎么设置?6步教你快速搞定!
  15. springBoot接入阿里云oss
  16. GitHub 入门教程
  17. Elasticsearch 多种跨机房灾备方案对比与实战解读
  18. python爬虫--小说爬取
  19. 零信任网络安全——软件定义边界SDP技术架构指南
  20. JQuery对CheckBox的一些相关操作

热门文章

  1. c语言青蛙跳答案是多少啊,青蛙跳台阶问题(示例代码)
  2. floyd算法----牛栏
  3. 英语计算机四级成绩查询入口,全国计算机四级考试成绩查询入口
  4. 仿苹果手机_安卓党福音?安卓手机也能拥有iOS控制中心,十分炫酷
  5. 电脑开机提示计算机无法启动不了,图文详解电脑开机无法进入系统怎么办
  6. html加载fbx模型,[Unity菜鸟] FBX模型动画提取
  7. Maya---捕捉命令
  8. linux磁盘写保护怎么修改_linux 文件、目录写保护
  9. nebula模拟器_nebula模拟器
  10. 海康威视web开发包开发使用说明