Chinaunix首页 | 论坛 | 博客
  • 博客访问: 291459
  • 博文数量: 68
  • 博客积分: 1474
  • 博客等级: 上尉
  • 技术积分: 616
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-12 12:07
文章分类

全部博文(68)

文章存档

2011年(68)

分类: 系统运维

2011-07-16 09:42:58

下面是使用PHP解析google天气的代码

  1. function fromGoogle($city){
  2.     $city = mb_convert_encoding($city, 'UTF-8', 'gb2312');
  3.     $city = Pinyin($city,1);
  4.     print_r("");
  5.     $content = file_get_contents("");
  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. }

阅读(1481) | 评论(0) | 转发(0) |
0

上一篇:PHP 发送邮件

下一篇:PHP 汉字转拼音

给主人留下些什么吧!~~