PHP web版各省市 三日内天气预报
程序员文章站
2022-03-02 21:05:01
...
<html> <head> <title>天气预报模块</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <link href="CSS/style.css" rel="stylesheet"> </head> <body> <?php //载入公共文件 require_once './common.php'; //调用省份,城市和代码的数组表文件 require_once S_ROOT.'./citycode.inc.php'; $country = ''; //初始化变量 $weather = array(); //定义数组 //如果传入城市编码参数,则根据该参数获取指定城市信息 if (isset($_GET['citycode']) && $_GET['citycode']!='' && strlen($_GET['citycode'])<6) { $city = getcitybycode($_GET['citycode'],$p,$c,$n); }elseif(isset($_COOKIE['citycode']) && $_COOKIE['citycode']!='') { $city = getcitybycode($_COOKIE['citycode'],$p,$c,$n); } unset($country, $p, $c, $n); //销毁指定的变量 $htmlarr = get_cma_html($city['0']); if(!is_array($htmlarr) || empty($htmlarr)){ die('读取*气象局天气信息失败!'); } //将读取的数据存储到XML中 $data_dir = DATAPATH.$city[3].'/'; $live_file = $data_dir.'live_'.$city['0'].'.xml'; //将读取的天气信息存储到XML文件中 $cma_file = $data_dir.'cma_'.$city['0'].'.xml'; //将读取的天气指数存储到XML文件中 if (!$cfg['cache']){ //无缓存,每次更新 $live_weather = get_live_weather($htmlarr); $cma_weather = get_cma_weather($htmlarr); } else { mk_dir($data_dir); if (!@file_exists($live_file)|| date("Ymd")>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); } ?> <table width="1004" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="258" height="144" rowspan="3" background="images/index_01.gif"> </td> <td width="746" height="50" valign="top" background="images/index_02.gif"> </td> </tr> <tr> <td width="746" height="34" valign="top" background="images/index_03.gif"> </td> </tr> <tr> <td width="746" height="60" valign="top" background="images/index_05.gif"> </td> </tr> <tr> <td colspan="2" valign="top"> <?php define("_TPLPath_", S_ROOT.'./templates/'); //定义模板路径 include template('weather.htm'); //调用模板文件 ?> </td> </tr> <tr> <td height="31" colspan="2" background="images/index_19.gif"> </td> </tr> </table> </body> </html>
<?php if(!defined('IN_WEATHER')) { exit('Access Denied'); } // 通过用户指定的城市编码获取该城市信息,$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 = 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"></li>', "\\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">(.+)*</td>',"\\2",$v)); } foreach ($gif['gif'] as $k => $v) { $weather['cma']['gif'][$k] = eregi_replace('(.+)*/tqyb/img/weather/(.+)*" width="20" height="20"> <img src="/tqyb/img/weather/(.+)*" width="20" height="20"></td>',"\\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="(.+)*">(.+)*</a></td>',"\\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 = '<?xml version="1.0" encoding="gb2312"?>'; $xmlstr .= "<weatherinfo>"; if ($mod == 1) { $xmlstr .= "<city>".$w['city']."</city>"; $datasize = count($w['cma']['date']); for($i=0; $i<$datasize; $i++){ $xmlstr .= "<data>"; $xmlstr .=" <date>".$w['cma']['date'][$i]."</date>"; $xmlstr .=" <weather>".$w['cma']['weather'][$i]."</weather>"; $xmlstr .=" <temperature>".$w['cma']['temperature'][$i]."</temperature>"; $xmlstr .=" <winddirect>".$w['cma']['winddirect'][$i]."</winddirect>"; $xmlstr .=" <gif>".$w['cma']['gif'][$i]."</gif>"; $xmlstr .="</data>"; } } elseif ($mod == 0) { foreach ($w as $k => $v) { $xmlstr .= "<".$k.">".$v."</".$k.">"; } } $xmlstr .="</weatherinfo>"; $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(); } ?>