免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1284 | 回复: 0
打印 上一主题 下一主题

PHP 解析google天气xml文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:54 |只看该作者 |倒序浏览
下面是使用PHP解析google天气的代码

  1. function fromGoogle($city){
  2.     $city = mb_convert_encoding($city, 'UTF-8', 'gb2312');
  3.     $city = Pinyin($city,1);
  4.     print_r("http://www.google.com/ig/api?hl=zh-cn&weather=$city");
  5.     $content = file_get_contents("http://www.google.com/ig/api?hl=zh-cn&weather=beijing");
  6.     $content || die("No such city's data");
  7.     $content = mb_convert_encoding($content, 'UTF-8', 'gb2312');
  8.     $xml = simplexml_load_string($content);
  9.     $date = $xml->weather->forecast_information->forecast_date->attributes();
  10.     
  11.     $forecastinfo = $xml->weather->forecast_information;
  12.     $cityname = $forecastinfo->city->attributes();
  13.     $postal_code = $forecastinfo->postal_code->attributes();
  14.     $forecast_date = $forecastinfo->forecast_date->attributes();
  15.     $current_date_time = $forecastinfo->current_date_time->attributes();
  16.     
  17.     $current = $xml->weather->current_conditions;
  18.      $condition = $current->condition->attributes();
  19.      $temp_c = $current->temp_c->attributes();
  20.      $humidity = $current->humidity->attributes();
  21.      $icon = $current->icon->attributes();
  22.      $windMsg = $current->wind_condition->attributes();
  23.      $condition && $condition = $xml->weather->forecast_conditions->condition->attributes();
  24.     
  25.      $today_info[currentdate] = $forecast_date;
  26.      $today_info[currenttime] = $current_date_time;
  27.      $today_info[cityname] = $postal_code;
  28.      $today_info[humidity] = $humidity;
  29.      $today_info[windMsg] = $windMsg;
  30.      $today_info[condition] = $condition;
  31.      $today_info[tempcur] = $temp_c;
  32.      $i = 0;
  33.     
  34.      foreach($xml->weather->forecast_conditions as $forecast) {
  35.          $temp = null;
  36.          $temp[low] = $forecast->low->attributes();
  37.          $temp[high] = $forecast->high->attributes();
  38.          $temp[conditionMsg] = $forecast->condition->attributes();
  39.          $temp[day_of_week] = $forecast->day_of_week->attributes();
  40.          $i++;
  41.          if($i == 1){
  42.              $today_info = array_merge($today_info,$temp);
  43.          }else if($i == 2 ){
  44.              $SeconDay_info = array_merge($SeconDay_info,$temp);
  45.          }else if($i == 3 ){
  46.              $ThirdDay_info = array_merge($ThirdDay_info,$temp);
  47.          }else if($i == 4 ){
  48.              $FourthDay_info = array_merge($FourthDay_info,$temp);
  49.          }
  50.      }
  51.      $todayall_info = Array();
  52.      $todayall_info[0] = $today_info;
  53.      $todayall_info[1] = $SeconDay_info;
  54.      $todayall_info[2] = $ThirdDay_info;
  55.      $todayall_info[3] = $FourthDay_info;
  56.      return $todayall_info;
  57. }

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP