<?php
/*
* 日期: 2008.04.17
*/
class weather{
public function __construct(){}
/*
* 功能: 获取访问者ip
* 说明: 无参数,成功返回访问者ip字符串,失败返回null
*/
function getip(){
if ($_SERVER["HTTP_X_FORWARDED_FOR"])
{
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif ($_SERVER["HTTP_CLIENT_IP"])
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
elseif ($_SERVER["REMOTE_ADDR"])
{
$ip = $_SERVER["REMOTE_ADDR"];
}
elseif (getenv("HTTP_X_FORWARDED_FOR"))
{
$ip = getenv("HTTP_X_FORWARDED_FOR");
}
elseif (getenv("HTTP_CLIENT_IP"))
{
$ip = getenv("HTTP_CLIENT_IP");
}
elseif (getenv("REMOTE_ADDR"))
{
$ip = getenv("REMOTE_ADDR");
}
else
{
$ip = null;
}
return $ip;
}
/*
* 功能: 通过用户指定的城市编码获取该城市信息
* 参数: $citycode为城市编码,$p为省份数组,$c为城市数组,$n为城市编码数组
*/
function getcitybycode($citycode,$p,$c,$n){
$flag = true;
foreach($n as $k => $arr){
if(in_array($citycode,$arr)){
$pkey = $k;
$flag = false;
break;
}
}
if($flag){
$pkey = '0';
$ckey = '0';
$citycode = $n[$pkey][$ckey];
}else{
$ckeyarr = array_keys($n[$pkey],$citycode);
$ckey = $ckeyarr[0];
}
$city = array($citycode,$p[$pkey],$c[$pkey][$ckey],$pkey);
return $city;
}
/*
* 功能: 通过用户ip得到用户所在位置,然后获取该城市信息
* 参数: $citycode为城市编码,$p为省份数组,$c为城市数组,$n为城市编码数组
*/
function getcitybyip($country,$p,$c,$n){
$pkey = $this->getkey($p,$country); //获取省份数组下标
$ckey = $this->getkey($c[$pkey],$country); //获取城市数组下标
$code = $n[$pkey][$ckey]; //取得相应省份所属的城市在中央天气预报信息库中的编码
$city= array($code,$country,$c[$pkey][$ckey],"$pkey");
return $city;
}
function getkey($arr,$country){
if(is_array($arr)){
foreach($arr as $key => $val){
if(preg_match('/('.$val.')/',$country,$match)){
return $key;
}
}
}else{
if(preg_match('/('.$arr.')/',$country,$match)){
return $key;
}
}
return 0;
}
/*
* 功能: 通过城市编码从中央气象局天气预报页面抓取天气信息
* 参数: $citycode为城市编码
* 说明: 返回天气信息数组
*/
function get_cma_weather($citycode){
if(!$htmlarr=file('http://www.cma.gov.cn/tqyb/weatherdetail/'.$citycode.'.html')){
//unset($weather);
return $this->get_cma_weather($citycode);
}
list($dateregine,$weather['city']) = explode(" ",strip_tags($htmlarr['861']));
$arr['date'] = array($htmlarr['871'],$htmlarr['872'],$htmlarr['873']);
$arr['weather'] = array($htmlarr['883'],$htmlarr['890'],$htmlarr['897']);
$arr['temperature'] = array($htmlarr['917'],$htmlarr['918'],$htmlarr['919']);
$winddirect = array($htmlarr['925'],$htmlarr['926'],$htmlarr['927']);
unset($htmlarr);
foreach($arr as $k => $v){
foreach($v as $key => $val){
$weather[$k][$key] = strip_tags($val);
}
}
foreach($winddirect as $k => $v){
$weather['winddirect'][$k] = htmlspecialchars(eregi_replace('(.+)*"b-cn">(.+)*</td>',"\\2",$v));
}
return $weather;
}
/*
* 功能: 从指定的路径读取存储有天气信息的xml文件
* 参数: $filepath为文件路径
* 说明: 返回天气信息数组
*/
function read_weather_xml($filepath){
$dom = new DomDocument();
$dom->load($filepath);
$xp = new DOMXPath($dom);
$citynodes = $dom->getElementsByTagName("city");
$nodes['date'] = $dom->getElementsByTagName("date");
$nodes['weather'] = $dom->getElementsByTagName("weather");
$nodes['temperature'] = $dom->getElementsByTagName("temperature");
$nodes['winddirect'] = $dom->getElementsByTagName("winddirect");
$weather['city'] = iconv("utf-8","gbk",$citynodes->item(0)->textContent);
foreach($nodes as $k => $v){
foreach($v as $key => $val){
$weather[$k][$key] = iconv("utf-8","gbk",$val->textContent);
}
}
//$weather['date'] = $xp->query("/weatherinfo/weather/date");
//$weather['weather'] = $xp->query("/weatherinfo/weather/weather");
//print_r($weather);
return $weather;
}
/*
* 功能: 将天气信息存储为指定的xml文件
* 参数: $cityweather,该城市天气信息$filepath为文件路径
*/
function save_weather_xml($cityweather,$filepath){
$dom = new DomDocument();
$xmlstr = '<?xml version="1.0" encoding="gb2312"?>';
$xmlstr .= "<weatherinfo>";
$xmlstr .= "<city>".$cityweather['city']."</city>";
$datasize = count($cityweather['date']);
for($i=0; $i<$datasize; $i++){
$xmlstr .= "<data>";
$xmlstr .=" <date>".$cityweather['date'][$i]."</date>";
$xmlstr .=" <weather>".$cityweather['weather'][$i]."</weather>";
$xmlstr .=" <temperature>".$cityweather['temperature'][$i]."</temperature>";
$xmlstr .=" <winddirect>".$cityweather['winddirect'][$i]."</winddirect>";
$xmlstr .="</data>";
}
$xmlstr .="</weatherinfo>";
$dom->loadXML($xmlstr);
$dom->save($filepath);
}
/*
* 功能: 获取天气信息
* 参数: $city为天气信息数组
*/
function getweather($city){
$pdir = WROOT.'/data/'.$city['3'];
if(!file_exists($pdir)){
mkdir($pdir,0777);
}
$filepath = $pdir.'/'.$city['0'].'.xml';
if(!file_exists($filepath)){
$cityweather = $this->get_cma_weather($city['0']);
$this->save_weather_xml($cityweather,$filepath);
}else if(date("Ymd")>date("Ymd",filemtime($filepath))){
unlink($filepath);
$cityweather = $this->get_cma_weather($city['0']);
$this->save_weather_xml($cityweather,$filepath);
}
return $this->read_weather_xml($filepath);
}
/*
* 功能: 将从xml文件中获取的天气信息用指定的模板显示出来
* 参数: $weather为天气信息数组,$phtml为模板文件,$model等于1获取当日天气,等于2获取48小时天气,
* 等于3回去72小时天气
*/
function weather_tmp_html($weather,$phtml,$model='3'){
if($model=='1'){
$array = array($weather['city'],$weather['date'][0],$weather['weather'][0],$weather['temperature'][0],$weather['winddirect'][0]);
$replace_array = array('<{city}>','<{date}>','<{weather}>','<{temperature}>','<{winddirect}>');
}else if($model=='2'){
$array = array($weather['city'],$weather['date'][0],$weather['weather'][0],$weather['temperature'][0],$weather['winddirect'][0],$weather['date'][1],$weather['weather'][1],$weather['temperature'][1],$weather['winddirect'][1]);
$replace_array = array('<{city}>','<{date}>','<{weather}>','<{temperature}>','<{winddirect}>','<{date1}>','<{weather1}>','<{temperature1}>','<{winddirect1}>');
}else{
$array = array($weather['city'],$weather['date'][0],$weather['weather'][0],$weather['temperature'][0],$weather['winddirect'][0],$weather['date'][1],$weather['weather'][1],$weather['temperature'][1],$weather['winddirect'][1],$weather['date'][2],$weather['weather'][2],$weather['temperature'][2],$weather['winddirect'][2]);
$replace_array = array('<{city}>','<{date}>','<{weather}>','<{temperature}>','<{winddirect}>','<{date1}>','<{weather1}>','<{temperature1}>','<{winddirect1}>','<{date2}>','<{weather2}>','<{temperature2}>','<{winddirect2}>');
}
if($html=file_get_contents($phtml)){
$html = str_replace($replace_array,$array,$html);
}else{
$html = '获取天气信息失败';
}
return $html;
}
}
?>