Chinaunix首页 | 论坛 | 博客
  • 博客访问: 312239
  • 博文数量: 26
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1631
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-09 00:51
个人简介

努力 奋斗 专注于linux内核 微信:tolimit 扣扣:348958453

文章分类
文章存档

2015年(26)

分类: LINUX

2015-01-26 09:37:18

新浪天气API使用方法

    API地址:

  红色标记为城市代码(也就是城市的中文转为GB2312的十六进制代码,比如北京对应的GB2312十六进制代码为B1B1BEA9),实际上需要查哪个城市就把红色标记改为对应城市代码即可.而实际上打开此url后对应的是一个xml文件,里面包括了此城市的天气信息.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- published at 2014-10-31 17:08:03 -->
  3. <Profiles>
  4. <Weather>
  5. <city>北京</city>
  6. <status1>小雨</status1>
  7. <status2>多云</status2>
  8. <figure1>xiaoyu</figure1>
  9. <figure2>duoyun</figure2>
  10. <direction1>无持续风向</direction1>
  11. <direction2>无持续风向</direction2>
  12. <power1>≤3</power1>
  13. <power2>≤3</power2>
  14. <temperature1>14</temperature1>
  15. <temperature2>9</temperature2>
  16. <ssd>5</ssd>
  17. <tgd1>16</tgd1>
  18. <tgd2>16</tgd2>
  19. <zwx>2</zwx>
  20. <ktk>6</ktk>
  21. <pollution>3</pollution>
  22. <xcz></xcz>
  23. <zho></zho>
  24. <diy></diy>
  25. <fas></fas>
  26. <chy>4</chy>
  27. <zho_shuoming>暂无</zho_shuoming>
  28. <diy_shuoming>暂无</diy_shuoming>
  29. <fas_shuoming>暂无</fas_shuoming>
  30. <chy_shuoming>套装、夹衣、风衣、夹克衫、西服套装、马甲衬衫配长裤</chy_shuoming>
  31. <pollution_l>轻度</pollution_l>
  32. <zwx_l></zwx_l>
  33. <ssd_l>略凉</ssd_l>
  34. <fas_l>暂无</fas_l>
  35. <zho_l>暂无</zho_l>
  36. <chy_l>夹衣类</chy_l>
  37. <ktk_l>适宜开启(制热)</ktk_l>
  38. <xcz_l>暂无</xcz_l>
  39. <diy_l>暂无</diy_l>
  40. <pollution_s>对空气污染物扩散无明显影响</pollution_s>
  41. <zwx_s>紫外线弱</zwx_s>
  42. <ssd_s>感觉有些凉,但是凉意微薄,不影响户外活动的开展。</ssd_s>
  43. <ktk_s>适宜开启空调</ktk_s>
  44. <xcz_s>暂无</xcz_s>
  45. <gm>2</gm>
  46. <gm_l>易发期</gm_l>
  47. <gm_s>天气很凉,季节转换的气候,慎重增加衣服;较易引起感冒;</gm_s>
  48. <yd>5</yd>
  49. <yd_l>不适宜</yd_l>
  50. <yd_s>虽然晴空万里,但是天气较凉,多数人不适宜户外运动;</yd_s>
  51. <savedate_weather>2014-10-31</savedate_weather>
  52. <savedate_life>2014-10-31</savedate_life>
  53. <savedate_zhishu>2014-10-31</savedate_zhishu>
  54. </Weather>
  55. </Profiles>

  现在我们的工作就是使用curl打开这个xml文件并且将其转换为我们需要的数据结构.

 

  先看看我们自定义的数据结构,用于存放天气信息(感觉还有更好的定义方法):


  1. #ifndef __SINA_WEATHER__
  2. #define __SINA_WEATHER__

  3. #include <stdio.h>

  4. #define FALSE (0)
  5. #define TRUE (!0)

  6. #define SINA_WEATHER_URL_HEAD ""
  7. #define SINA_WEATHER_URL_TAIL "&password=DJOYnieT8234jlsK&day=0"

  8. struct date {
  9.     short year;
  10.     short month;
  11.     short day;
  12. };

  13. struct weather_info {
  14.     char * city;

  15.     //天气情况(中文)
  16.     char * status1;
  17.     char * status2;

  18.     //天气情况(拼音)
  19.     char * figure1;
  20.     char * figure2;

  21.     //风向
  22.     char * direction1;
  23.     char * direction2;

  24.     //风级
  25.     char * power1;
  26.     char * power2;

  27.     //温度
  28.     int temperature1;
  29.     int temperature2;

  30.     //体感指数数值
  31.     int ssd;
  32.     //体感度指数
  33.     char * ssd_l;
  34.     //体感度指数说明
  35.     char * ssd_s;

  36.     //体感温度
  37.     int tgd1;
  38.     int tgd2;

  39.     //紫外线指数数值
  40.     int zwx;
  41.     //紫外线指数
  42.     char * zwx_l;
  43.     //紫外线指数说明
  44.     char * zwx_s;

  45.     //空调指数数值
  46.     int ktk;
  47.     //空调指数
  48.     char * ktk_l;
  49.     //空调指数说明
  50.     char * ktk_s;

  51.     //污染指数数值
  52.     int pollution;
  53.     //污染扩散条件
  54.     char * pollution_l;
  55.     //污染指数说明
  56.     char * pollution_s;

  57.     //穿衣指数数值
  58.     int chy;
  59.     //穿衣指数
  60.     char * chy_l;
  61.     //穿衣说明
  62.     char * chy_shuoming;

  63.     //洗车指数数值
  64.     int xcz;
  65.     //洗车指数
  66.     char * xcz_l;
  67.     //洗车指数说明
  68.     char * xcz_s;


  69.     //感冒指数数值
  70.     int gm;
  71.     //感冒指数
  72.     char * gm_l;
  73.     //感冒指数说明
  74.     char * gm_s;

  75.     //运动指数数值
  76.     int yd;
  77.     //运动指数
  78.     char * yd_l;
  79.     //运动指数说明
  80.     char * yd_s;

  81.     struct date weather_date;
  82.     struct date life_date;
  83.     struct date zhishu_date;
  84. };

  85. struct weather_info * get_weather_info_from_sina (const char * city_name);

  86. #endif

  先说一下流程,首先设计想法是就定义一个接口,调用时只需要输入一个城市名称,则自动返回此城市的天气信息结构体,而在内部,分别执行了UTF-8转GB2312格式curl初始化获取天气XML信息填充struct weather_info结构体.

  流程图:




代码(sina_weather.c):


  1. #include <stdio.h>
  2. #include <curl/curl.h>
  3. #include <iconv.h>

  4. #include "sina_weather.h"

  5. //根据key获取xml中对应的值
  6. char * get_xml_key_value (const char * xml, char * key)
  7. {
  8.     char * head;
  9.     char * tail;
  10.     char stamp[1024];
  11.     char * value;

  12.     sprintf(stamp, "<%s>", key);
  13.     if ((head = strstr (xml, stamp)) == NULL)
  14.         return NULL;

  15.     head = head + strlen (stamp);
  16.     while (isspace (head[0]))
  17.         head ++;

  18.     sprintf(stamp, "", key);

  19.     if ((tail = strstr (head, stamp)) == NULL)
  20.         return NULL;

  21.     while (isspace(tail[-1]) && (tail > head))
  22.         tail --;

  23.     if (tail > head){
  24.         value = calloc (tail - head + 1, 1);
  25.         memcpy (value, head, tail - head);
  26.         value[tail - head] = 0x00;
  27.     }

  28.     return value;
  29. }


  30. //UTF-8转GB2312
  31. int utf8_to_gb2312 (const char * src, char * dest_buf)
  32. {
  33.     iconv_t con;
  34.     int src_length = strlen (src);

  35.     if( (con = iconv_open ("gb2312", "utf-8")) ==0 )
  36.           return FALSE;

  37.     memset (dest_buf, 0, src_length);

  38.     if(-1 == iconv (con, &src, &src_length, &dest_buf, &src_length))
  39.          return FALSE;
  40.     iconv_close (con);

  41.     return TRUE;
  42. }

  43. //将一个GB2312字符串转换为它的十六进制字符串(每个字符用%隔开)
  44. char * str_convert_to_gb2312_hex (const char * src)
  45. {
  46.     if (src == NULL)
  47.         return NULL;

  48.     int length = strlen (src) - 2;
  49.     int index = 0;
  50.     int num = 0;
  51.     char * gb_str = calloc (((length) + 1), 1);
  52.     char * hex_str = calloc ((length * 3) + 1, 1);

  53.     utf8_to_gb2312 (src, gb_str);
  54.     while ((index / 3) < length) {
  55.         hex_str[index] = '%';
  56.         sprintf (&hex_str[index + 1], "%02X", gb_str[index / 3] & 0xff);
  57.         index = index + 3;
  58.     }
  59.     hex_str[length * 3] = '\0';
  60.     return hex_str;
  61. }

  62. void convert_to_gb2312_clean (char * str)
  63. {
  64.     free (str);
  65. }

  66. //curl下载时的回调函数,当下载到数据时会调用此函数进行操作
  67. size_t down_data_callback (void * ptr, size_t size, size_t nmemb, void * user_buf)
  68. {
  69.     strcat (user_buf, ptr);
  70.     return size * nmemb;
  71. }

  72. uint64_t curl_get_content (const char * url, char * content)
  73. {
  74.     if ((url == NULL) || (content == NULL))
  75.         return;

  76.     CURL * curl = curl_easy_init ();
  77.     curl_easy_setopt (curl, CURLOPT_URL, url); //设置curl的目标url地址
  78.     curl_easy_setopt (curl, CURLOPT_TIMEOUT, 15); //下载超时时间
  79.     curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1); //屏蔽其他信号
  80.     curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, down_data_callback); //设置下载回调函数
  81.     curl_easy_setopt (curl, CURLOPT_WRITEDATA, content); //设置下载数据保存缓冲区(此参数会传到down_data_callback的user_buf)

  82.     CURLcode retval = curl_easy_perform (curl);
  83.     if (retval == CURLE_OK) {
  84.         double down_length = 0;
  85.         curl_easy_getinfo (curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &down_length);
  86.         curl_easy_cleanup (curl);
  87.         return (uint64_t)down_length;
  88.     }
  89.     curl_easy_cleanup (curl);
  90.     return 0;
  91. }

  92. struct weather_info * weather_info_get_from_xml (const char * xml, struct weather_info * w_info)
  93. {
  94.     if ((xml == NULL) || (w_info == NULL))
  95.         return w_info;

  96.     char * str_data = NULL;

  97.     w_info->city = get_xml_key_value (xml, "city");
  98.     w_info->status1 = get_xml_key_value (xml, "status1");
  99.     w_info->status1 = get_xml_key_value (xml, "status1");
  100.     str_data = get_xml_key_value (xml, "temperature1");
  101.     w_info->temperature1 = (int) atoi (str_data);
  102.     free (str_data);
  103.     str_data = get_xml_key_value (xml, "temperature2");
  104.     w_info->temperature2 = (int) atoi (str_data);
  105.     free (str_data);
  106.   //此处省略N多,基本都同上操作.
  107. }

  108. struct weather_info * get_weather_info_from_sina (const char * city_name)
  109. {
  110.     if (city_name == NULL)
  111.         return NULL;

  112.     CURL * curl = NULL;
  113.     CURLcode retval = 0;
  114.     char xml_data[4096];
  115.     char url[1024] = SINA_WEATHER_URL_HEAD;
  116.     char * city_hex = str_convert_to_gb2312_hex (city_name);
  117.     struct weather_info * sina_w_info = calloc (sizeof (struct weather_info), 1);

  118.     strcat (url, city_hex);
  119.     strcat (url, SINA_WEATHER_URL_TAIL);

  120.     convert_to_gb2312_clean (city_hex);

  121.     uint64_t down_size = curl_get_content (url, xml_data);

  122.     if (down_size == 0) {
  123.         free (sina_w_info);
  124.         return NULL;
  125.     }

  126.     weather_info_get_from_xml (xml_data, sina_w_info);
  127.     return sina_w_info;
  128. }
    
    
可以看出所有的代码封装成了一个struct weather_info * get_weather_info_from_sina (const char * city_name)当需要使用使用时直接调用此函数即返回城市天气结构体

CURL:

  或许有些同学不太清楚怎么在c中使用curl库,其实使用的方法很简单,就简单说明一下,在ubuntu下安装curl库命令如下

  1. # apt-get install libcurl-nss-dev
    在程序编译时需要加入curl库的选项:

  1. -lcurl
  使用curl下载时常规的代码顺序为:

  1. CURL * curl = curl_easy_init (); //初始化

  2.     curl_easy_setopt (curl, CURLOPT_URL, url); //具体设置代表的意思在http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
  3.     curl_easy_setopt (curl, CURLOPT_TIMEOUT, 15);
  4.     curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
  5.     curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, down_data_callback);
  6.     curl_easy_setopt (curl, CURLOPT_WRITEDATA, content);

  7.     CURLcode retval = curl_easy_perform (curl); //执行操作
  8.     if (retval == CURLE_OK) {
  9.     //添加所需要处理的代码
  10.     ........
  11.     }
  12.     curl_easy_cleanup (curl); //清空
    在使用curl下载时,会设置CURLOPT_WRITEFUNCTION,此设置主要是为了在下载有数据过来时,调用down_data_callback这个回调函数,并将数据保存到content这个缓冲区中.

  1. curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, down_data_callback);
  2. curl_easy_setopt (curl, CURLOPT_WRITEDATA, content);

  3. size_t down_data_callback (void * ptr, size_t size, size_t nmemb, void * user_buf)
  4. {
  5.   strcat (user_buf, ptr);
  6.   return size * nmemb;
  7. }


小结

    curl作为一个C的下载库还是非常方便使用的,其用法很简单,还能够支持断点续传,程序需要用到下载功能的同学可以试试curl库,而现在网上许多公共API接口形式都是与新浪天气API类似,比如google地球的API,百度地图API等都与它类似,这里只做一个入门,深入的话可以以此作为一个入口进行学习。

附录(1代表白天,2代表晚上): 

天气情况中文

天气情况拼音

风向

风级

温度

体感指数数值

体感度指数

体感度指数说明

体感温度

紫外线指数数值

紫外线指数

紫外线指数说明

空调指数数值

空调指数

空调指数说明

污染指数数值

污染物扩散条件

污染指数说明

洗车指数数值

洗车指数

洗车指数说明

穿衣指数数值

穿衣指数

穿衣说明

感冒指数数值

感冒指数

感冒指数说明

运动指数数值

运动指数

运动指数说明

 

 

 

 

 

 

 

 

 

天气预报日期

生活日期

指数日期






阅读(3746) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~