码迷,mamicode.com
首页 > 其他好文 > 详细

根据IP获取天气状况

时间:2015-05-08 09:32:34      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:php   ip   天气   

上一节我们学习了如何获取客户端IP,并确定其所在地。这一节我们接着学习根据已获取的信息进一步获取客户所在地的天气情况。

根据上一节里我们已经获取了的客户所在的省份、城市,那么我们只要利用一些与天气相关的网站的API,就能完成天气情况的信息获取(此处使用的是中国天气网所提供的API)。

我们还是先来看看代码怎么实现的:

<?php
header('Content-Type:text/html;Charset=utf-8');
function GetIp(){
    $realip = '';
    $unknown = 'unknown';
    if (isset($_SERVER)){
        if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)){
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            foreach($arr as $ip){
                $ip = trim($ip);
                if ($ip != 'unknown'){
                    $realip = $ip;
                    break;
                }
            }
        }else if(isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP']) && strcasecmp($_SERVER['HTTP_CLIENT_IP'], $unknown)){
            $realip = $_SERVER['HTTP_CLIENT_IP'];
        }else if(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)){
            $realip = $_SERVER['REMOTE_ADDR'];
        }else{
            $realip = $unknown;
        }
    }else{
        if(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), $unknown)){
            $realip = getenv("HTTP_X_FORWARDED_FOR");
        }else if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), $unknown)){
            $realip = getenv("HTTP_CLIENT_IP");
        }else if(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), $unknown)){
            $realip = getenv("REMOTE_ADDR");
        }else{
            $realip = $unknown;
        }
    }
    $realip = preg_match("/[\d\.]{7,15}/", $realip, $matches) ? $matches[0] : $unknown;
    return $realip;
}

function GetIpLookup($ip = ''){
    if(empty($ip)){
        $ip = GetIp();
    }
    $address = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);
    if(empty($address)){ return false; }
    $jsonMatches = array();
    preg_match('#\{.+?\}#', $address, $jsonMatches);
    if(!isset($jsonMatches[0])){ return false; }
    $address_json = json_decode($jsonMatches[0], true);
    if(isset($address_json['ret']) && $address_json['ret'] == 1){
        $address_json['ip'] = $ip;
        unset($address_json['ret']);
    }else{
        return false;
    }
    return $address_json;
}

$ipInfos = GetIpLookup();
//var_dump($ipInfos);
//echo "<br>";
echo "IP所在地: " .$ipInfos ["country"].$ipInfos ["province"].$ipInfos ["city"]. "<br>";

//获取省级代码
$get_province_url = "http://www.weather.com.cn/data/list3/city.xml?level=1";
$all_province_info = file_get_contents($get_province_url);
//echo $all_province_info."<br>";
//切割为数组
$all_province_arr = explode(',', $all_province_info);
//var_dump($all_province_arr);
//echo"<br>";
$city_code;
foreach($all_province_arr as $key => $row){
    $temp = explode('|', $row);
    if($ipInfos ["province"] == $temp[1]){
        $city_code = $temp[0];
    }
}
//echo $city_code."<br>";
//获取城市代码
$all_city_info = file_get_contents("http://www.weather.com.cn/data/list3/city".$city_code.".xml?level=2");
$all_city_arr = explode(',', $all_city_info);
foreach($all_city_arr as $key => $row){
    $temp = explode('|', $row);
    if($ipInfos ["city"] == $temp[1]){
        $city_code = $temp[0];
    }
}
//echo $city_code."<br>";
//获取区域代码
$all_county_info = file_get_contents("http://www.weather.com.cn/data/list3/city".$city_code.".xml?level=2");
$all_county_arr = explode(',', $all_county_info);
$county_arr = explode('|', $all_county_arr[0]);
$city_code = $county_arr[0];
//echo $city_code."<br>";
//获取天气
$url = "http://www.weather.com.cn/data/cityinfo/101".$city_code.".html";
$weather_info = file_get_contents($url);
//var_dump($weather_info);
//echo "<br>";
$str = substr($weather_info,0,-5);
//var_dump($str);
//echo "<br>";
$weather_arr = json_decode($str);
//var_dump($weather_arr);

echo "气温: " .$weather_arr->weatherinfo->temp2."到".$weather_arr->weatherinfo->temp1."<br>"."天气: ".$weather_arr->weatherinfo->weather. "<br>";
?>

运行结果如下:

技术分享

需要注意的地方有:

1、使用中国天气网提供的API之前,需要做的工作时获取所在地的省份代码、城市代码和区域代码。

2、获取省份代码时,返回的字符串格式为

01|北京,02|上海,03|天津,04|重庆,05|黑龙江,06|吉林,07|辽宁,08|内蒙古,09|河北,10|山西,11|陕西,12|山东,13|新疆,14|西藏,15|青海,16|甘肃,17|宁夏,18|河南,19|江苏,20|湖北,21|浙江,22|安徽,23|福建,24|江西,25|湖南,26|贵州,27|四川,28|广东,29|云南,30|广西,31|海南,32|香港,33|澳门,34|台湾
需要用explode() 函数把字符串分割为数组:

array(34) { [0]=> string(9) "01|北京" [1]=> string(9) "02|上海" [2]=> string(9) "03|天津" [3]=> string(9) "04|重庆" [4]=> string(12) "05|黑龙江" [5]=> string(9) "06|吉林" [6]=> string(9) "07|辽宁" [7]=> string(12) "08|内蒙古" [8]=> string(9) "09|河北" [9]=> string(9) "10|山西" [10]=> string(9) "11|陕西" [11]=> string(9) "12|山东" [12]=> string(9) "13|新疆" [13]=> string(9) "14|西藏" [14]=> string(9) "15|青海" [15]=> string(9) "16|甘肃" [16]=> string(9) "17|宁夏" [17]=> string(9) "18|河南" [18]=> string(9) "19|江苏" [19]=> string(9) "20|湖北" [20]=> string(9) "21|浙江" [21]=> string(9) "22|安徽" [22]=> string(9) "23|福建" [23]=> string(9) "24|江西" [24]=> string(9) "25|湖南" [25]=> string(9) "26|贵州" [26]=> string(9) "27|四川" [27]=> string(9) "28|广东" [28]=> string(9) "29|云南" [29]=> string(9) "30|广西" [30]=> string(9) "31|海南" [31]=> string(9) "32|香港" [32]=> string(9) "33|澳门" [33]=> string(9) "34|台湾" }

3、返回天气信息json格式不对:

string(164) "{"weatherinfo":{"city":"成都","cityid":"101270101","temp1":"21℃","temp2":"14℃","weather":"阴转小雨","img1":"d2.gif","img2":"n7.gif","ptime":"08:00"}}ml> " 

后面多了几个不必要的字符,要进行字符串处理 substr($weather_info,0,-5):

string(159) "{"weatherinfo":{"city":"成都","cityid":"101270101","temp1":"21℃","temp2":"14℃","weather":"阴转小雨","img1":"d2.gif","img2":"n7.gif","ptime":"08:00"}}" 

最后对 JSON 格式的字符串进行编码:

object(stdClass)#13 (1) { ["weatherinfo"]=> object(stdClass)#14 (8) { ["city"]=> string(6) "成都" ["cityid"]=> string(9) "101270101" ["temp1"]=> string(5) "21℃" ["temp2"]=> string(5) "14℃" ["weather"]=> string(12) "阴转小雨" ["img1"]=> string(6) "d2.gif" ["img2"]=> string(6) "n7.gif" ["ptime"]=> string(5) "08:00" } } 


本文工作都是由于个人兴趣,还存在一些地方需要改进,还请大家多多指正。

转载请注明出处:



根据IP获取天气状况

标签:php   ip   天气   

原文地址:http://blog.csdn.net/liuruiqun/article/details/45568759

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!