天气预报模块 date("Ymd",filemtime($live_file)) || ($_SGLOBAL['timestamp']-filemtime($live_file))>$cfg['lw_cachetime']) { @unlink($live_file); //获取实况天气信息,并保存 $live_weather = get_live_weather($htmlarr); save_weather_xml($live_weather, $live_file, 0); } else { $live_weather = read_weather_xml($live_file, 0); } if (!@file_exists($cma_file) || date("Ymd")>date("Ymd",filemtime($cma_file)) || ($_SGLOBAL['timestamp']-filemtime($cma_file))>$cfg['w_cachetime']) { @unlink($cma_file); //获取未来三日天气信息和指数查询,并保存 $cma_weather = get_cma_weather($htmlarr); save_weather_xml($cma_weather, $cma_file, 1); } else { $cma_weather = read_weather_xml($cma_file, 1); } } unset($htmlarr); //销毁指定的变量 foreach ($cma_weather['cma']['gif'] as $k => $v) { //遍历天气数组元素 list($gif[$k][0],$gif[$k][1]) = explode(',', $v); } ?>
$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 = getkey($p,$country); //获取省份数组下标 $ckey = 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; } function mk_dir($dir) { //创建文件夹,@param $dir 文件夹路径 if(!@file_exists($dir)) { @mkdir($dir, 0777); } } /** * 功能: 通过城市编码从中国气象局天气预报页面读取信息 * 参数: $citycode为城市编码 * 说明: 返回包含天气信息数组 */ function get_cma_html($citycode) { for ($i=0; $i<10; $i++) { //中国气象局天气数据 if (false != ($htmlarr = file('http://www.cma.gov.cn/tqyb/weatherdetail/'.$citycode.'.html'))) { return $htmlarr; } } return false; } /** * 功能: 从天气数组中提取实况天气信息 * 参数: $htmlarr为天气信息文本 * 说明: 返回实况天气信息数组 */ function get_live_weather($htmlarr) { $weather = array(); $weather['date'] = strip_tags($htmlarr[660]); $weather['city'] = strip_tags($htmlarr[670]); $weather['weather'] = strip_tags($htmlarr[674]); $weather['temperature'] = strip_tags($htmlarr[675]); $weather['wind'] = strip_tags($htmlarr[681]); $weather['air'] = strip_tags($htmlarr[682]); $weather['ultraviolet'] = strip_tags($htmlarr[683]); $weather['a'] = strip_tags($htmlarr[684]); $weather['gif'] = eregi_replace('(.+)*/tqyb/img/weather/(.+)*" width="20" height="20">', "\\2", $htmlarr[671]); return $weather; } /** * 功能: 从天气数组中提取三日天气信息 * 参数: $htmlarr为天气信息文本 * 说明: 返回天气信息数组 */ function get_cma_weather($htmlarr){ $waather = array(); 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']); $gif['gif'] = array($htmlarr['882'],$htmlarr['889'],$htmlarr['896']); $winddirect = array($htmlarr['925'],$htmlarr['926'],$htmlarr['927']); foreach($arr as $k => $v){ foreach($v as $key => $val){ $weather['cma'][$k][$key] = strip_tags($val); } } foreach($winddirect as $k => $v){ $weather['cma']['winddirect'][$k] = htmlspecialchars(eregi_replace('(.+)*"b-cn">(.+)*',"\\2",$v)); } foreach ($gif['gif'] as $k => $v) { $weather['cma']['gif'][$k] = eregi_replace('(.+)*/tqyb/img/weather/(.+)*" width="20" height="20"> ',"\\3,\\2",$v); } return $weather; } /** * 功能: 从文本中提取指数查询 * 参数: $htmlarr为天气信息文本 * 说明: 返回指数查询数组 */ function get_zscx_weather($htmlarr) { $zscx = array('cyzs'=>$htmlarr['974'],'gmzs'=>$htmlarr['978'],'clzs'=>$htmlarr['982'],'jtzs'=>$htmlarr['986'],'zszs'=>$htmlarr['990'],'gyzs'=>$htmlarr['994'],'fszs'=>$htmlarr['998'],'lxzs'=>$htmlarr['1002']); foreach ($zscx as $k => $v) { $weather[$k] = eregi_replace('(.+)*title="(.+)*">(.+)*',"\\2",$v); } return $weather; } /** * 功能: 从指定的路径读取存储有天气信息的xml文件 * 参数: $filepath为文件路径 * 说明: 返回天气信息数组 */ function read_weather_xml($filepath, $mod=0){ $w = simplexml_load_file($filepath); if ($mod == 0) { $weather['city'] = $w->city; $weather['date'] = $w->date; $weather['weather'] = $w->weather; $weather['temperature'] = $w->temperature; $weather['wind'] = $w->wind; $weather['air'] = $w->air; $weather['ultraviolet'] = $w->ultraviolet; $weather['gif'] = $w->gif; foreach ($weather as $k => $v) { $weather[$k] = iconv("utf-8","gb2312",$v); } } elseif ($mod == 1) { $weather['city'] = iconv("utf-8","gb2312",$w->city); $i = 0; foreach ($w->data as $k => $v) { foreach ($v as $key => $val) { $weather['cma'][$key][$i] = iconv("utf-8","gb2312",$val); } $i++; } foreach ($w->zscx as $v){ foreach ($v as $key => $val) { $weather['zscx'][$key] = iconv("utf-8","gb2312",$val); } } } return $weather; } /** * 功能: 将天气信息存储为指定的xml文件 * 参数: $w为该城市天气信息,$filepath为文件路径,$mod=0是表示实况天气信息,$mod=1为未来三日天气信息 */ function save_weather_xml($w, $filepath, $mod=0){ $dom = new DomDocument(); $xmlstr = ''; $xmlstr .= ""; if ($mod == 1) { $xmlstr .= " "; $dom->loadXML($xmlstr); $dom->save($filepath); } //模板调用 function template($tplfile, $tplpath = '', $tplcachepath = '', $userpack = '', $userpackpath = '') { $tplpath = $tplpath != '' ? $tplpath : (defined("_TPLPath_") ? _TPLPath_ : ''); $tplfile = $tplpath.$tplfile; $tplcachelimit = defined("_TPLCacheLimit_") ? _TPLCacheLimit_ : 0; $cachefile = ($tplcachepath != '' ? $tplcachepath : (defined("_TPLCachePath_") ? _TPLCachePath_ : '')).str_replace(array('/', '.'), '_', $tplfile.($userpack ? '.'.($userpackpath ? $userpackpath : '').$userpack : '')).'.php'; $cachetime = @filemtime($cachefile); if (@filemtime($tplfile) <= $cachetime && (!$tplcachelimit || time() - $cachetime <= $tplcachelimit)) return $cachefile; include S_ROOT.'./libs/nemo.php'; $nemotpl = new nemo; $nemotpl->userpack = $userpack ? ($userpackpath ? $userpackpath : $tplpath).$userpack.'.php' : ''; $nemotpl->template = file_get_contents($tplfile); $nemotpl->cachefile = $cachefile; $nemotpl->extraparms = ',\\\''.$tplpath.'\\\',\\\''.$tplcachepath.'\\\',\\\''.$userpack.'\\\',\\\''.$userpackpath.'\\\''; return $nemotpl->compile(); } ?>".$w['city']." "; $datasize = count($w['cma']['date']); for($i=0; $i<$datasize; $i++){ $xmlstr .= ""; $xmlstr .="".$w['cma']['date'][$i]." "; $xmlstr .="".$w['cma']['weather'][$i]." "; $xmlstr .="".$w['cma']['temperature'][$i]." "; $xmlstr .="".$w['cma']['winddirect'][$i]." "; $xmlstr .="".$w['cma']['gif'][$i]." "; $xmlstr .=""; } } elseif ($mod == 0) { foreach ($w as $k => $v) { $xmlstr .= "<".$k.">".$v."".$k.">"; } } $xmlstr .="