天气预报集成函数(二)

2014-11-24 03:26:53 · 作者: · 浏览: 2
if( !isset($_COOKIE['user_city']) || !$_COOKIE['user_city']){ $city = $this->getCity($ip); setcookie('user_city', $city, time() + 99999999999, '/'); }else{ $city = $_COOKIE['user_city']; } $city = str_split($city,strlen($city)-3); $this->city = $city[0]; $isUtf8 = is_utf8($this->city) 1 : NULL; $this->citypy = Pinyin($this->city, $isUtf8); if( !isset($_COOKIE['citycode']) || !$_COOKIE['citycode']){ $cityUrl = "http://evenle.com/wei/20130921/city.php"; $web=json_decode(file_get_contents($cityUrl)); $arr=$this->json_to_array($web); $this->citycode = $arr[$this->city]; setcookie('citycode', $this->citycode, time() + 99999999999, '/'); }else{ $this->citycode = $_COOKIE['citycode']; } } } function getIP() { static $realip; if (isset($_SERVER)){ if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ $realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else if (isset($_SERVER["HTTP_CLIENT_IP"])) { $realip = $_SERVER["HTTP_CLIENT_IP"]; } else { $realip = $_SERVER["REMOTE_ADDR"]; } } else { if (getenv("HTTP_X_FORWARDED_FOR")){ $realip = getenv("HTTP_X_FORWARDED_FOR"); } else if (getenv("HTTP_CLIENT_IP")) { $realip = getenv("HTTP_CLIENT_IP"); } else { $realip = getenv("REMOTE_ADDR"); } } return $realip; } /** * 获取 IP 地理位置 * 淘宝IP接口 * @Return: array */ function getCity($ip) { $url="http://ip.taobao.com/service/getIpInfo.php ip=".$ip; $ip=json_decode(file_get_contents($url)); if((string)$ip->code=='1'){ return false; } $data = $ip->
data->city; return $data; } function getWeather($cityId='') { if(!$cityId){ $cityId = $this->citycode; } if($this->date != strtotime( date('Y-m-d') )){ $url="http://m.weather.com.cn/data/".$cityId.".html"; $_weather=file_get_contents($url); if($_weather){ $data = json_decode($_weather); $key = ( ($this->date-strtotime( date('Y-m-d') ))/86400 ) + 1; $data = $this->json_to_array($data); //天气的详细参数说明参考 $qiwen = 'temp' . $key; $tianqi = 'weather' . $key; $weather['qiwen'] = $data['weatherinfo'][$qiwen]; $weather['tianqi'] = $data['weatherinfo'][$tianqi]; } }else{ $url= 'http://www.weather.com.cn/data/sk/'.$cityId.'.html'; $_weather = file_get_contents($url); $weather = array(); if($_weather){ $data = json_decode($_weather); $weather['qiwen'] = $data->weatherinfo->temp.'℃'; } $url='http://www.weather.com.cn/data/cityinfo/'.$cityId.'.html'; $_weather = file_get_contents($url); if($_weather){ $data = json_decode($_weather); $weather['tianqi'] = $data->weatherinfo->weather; } } $air = $this->_getAir(); $weather['air'] = $air; return $weather; } function json_to_array($web){ $arr=array(); foreach($web as $k=>$w){ if(is_object($w)) $arr[$k]= $this->json_to_array($w); //判断类型是不是object else $arr[$k]=$w; } return $arr; } } function is_utf8($word) { if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word) == true || pre