- 论坛徽章:
- 0
|
下面是使用PHP解析google天气的代码
- function fromGoogle($city){
-
$city = mb_convert_encoding($city, 'UTF-8', 'gb2312');
-
$city = Pinyin($city,1);
-
print_r("http://www.google.com/ig/api?hl=zh-cn&weather=$city");
-
$content = file_get_contents("http://www.google.com/ig/api?hl=zh-cn&weather=beijing");
-
$content || die("No such city's data");
-
$content = mb_convert_encoding($content, 'UTF-8', 'gb2312');
-
$xml = simplexml_load_string($content);
-
$date = $xml->weather->forecast_information->forecast_date->attributes();
-
-
$forecastinfo = $xml->weather->forecast_information;
-
$cityname = $forecastinfo->city->attributes();
-
$postal_code = $forecastinfo->postal_code->attributes();
-
$forecast_date = $forecastinfo->forecast_date->attributes();
-
$current_date_time = $forecastinfo->current_date_time->attributes();
-
-
$current = $xml->weather->current_conditions;
-
$condition = $current->condition->attributes();
-
$temp_c = $current->temp_c->attributes();
-
$humidity = $current->humidity->attributes();
-
$icon = $current->icon->attributes();
-
$windMsg = $current->wind_condition->attributes();
-
$condition && $condition = $xml->weather->forecast_conditions->condition->attributes();
-
-
$today_info[currentdate] = $forecast_date;
-
$today_info[currenttime] = $current_date_time;
-
$today_info[cityname] = $postal_code;
-
$today_info[humidity] = $humidity;
-
$today_info[windMsg] = $windMsg;
-
$today_info[condition] = $condition;
-
$today_info[tempcur] = $temp_c;
-
$i = 0;
-
-
foreach($xml->weather->forecast_conditions as $forecast) {
-
$temp = null;
-
$temp[low] = $forecast->low->attributes();
-
$temp[high] = $forecast->high->attributes();
-
$temp[conditionMsg] = $forecast->condition->attributes();
-
$temp[day_of_week] = $forecast->day_of_week->attributes();
-
$i++;
-
if($i == 1){
-
$today_info = array_merge($today_info,$temp);
-
}else if($i == 2 ){
-
$SeconDay_info = array_merge($SeconDay_info,$temp);
-
}else if($i == 3 ){
-
$ThirdDay_info = array_merge($ThirdDay_info,$temp);
-
}else if($i == 4 ){
-
$FourthDay_info = array_merge($FourthDay_info,$temp);
-
}
-
}
-
$todayall_info = Array();
-
$todayall_info[0] = $today_info;
-
$todayall_info[1] = $SeconDay_info;
-
$todayall_info[2] = $ThirdDay_info;
-
$todayall_info[3] = $FourthDay_info;
-
return $todayall_info;
-
}
|
|