你的位置:LAMP之窗 >> 资讯 >> PHP >> 详细内容 在线投稿

php 利用Google Weather Api读取天气预报

热度32票  浏览123次 【共0条评论】【我要评论 时间:2010年3月30日 23:43
蘑菇今天研究了一下Google Weather Api,用php做了一个天气预报信息的demo,具体代码如下

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();
}
?>        
TAG: API Api google Google PHP php Weather 天气预报
顶:4 踩:2
对本文中的事件或人物打分:
当前平均分:-1.78 (9次打分)
对本篇资讯内容的质量打分:
当前平均分:-1.7 (10次打分)
【已经有7人表态】
上一篇 下一篇
发表评论

网友评论仅供网友表达个人看法,并不表明本网同意其观点或证实其描述。

查看全部回复【已有0位网友发表了看法】
       

网络资源