php 利用Google Weather Api读取天气预报
1,请阅读google Weather API调用规范
2,城市数据
数据库地址:http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
PHP程序下载地址:http://geolite.maxmind.com/download/geoip/api/php/
3,蘑菇的代码
<?php
//取用户真实ip
function getUserIP() {
if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$user_ip=$_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
$user_ip=$_SERVER["REMOTE_ADDR"];
}
return $user_ip;
}
include("geoip.inc");
include("geoipcity.inc");
$gi = geoip_open("yourpath/GeoIP.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,getUserIP());
//取得城市拼音 例如:Beijing。也可以取经纬度
$city = trim($record->city);
geoip_close($gi);
if($city){
//分析Google Weather Api接口数据
$xml = new DOMDocument();
$content = file_get_contents('http://www.google.com/ig/api?weather='.$city.'&hl=zh-cn');
$content || die("No such city's data");
$content = mb_convert_encoding($content, 'UTF-8', 'GBK');
$xml = simplexml_load_string($content);
$date = $xml->weather->forecast_information->forecast_date->attributes();
$city_name = $xml->weather->forecast_information->city->attributes();
$html = $date. "<br>\r\n";
$current = $xml->weather->current_conditions;
$condition = $current->condition->attributes();
$temp_c = $current->temp_c->attributes();
$humidity = $current->humidity->attributes();
$icon = $current->icon->attributes();
$wind_condition = $current->wind_condition->attributes();
$current_date_time = $xml->weather->forecast_information->current_date_time->attributes();
}
?>



