天气预报数据获取
时间:2007-01-03 来源:lib
?php
//error_reporting(0);
$config=array(
'id' => 59431, //天气的城市的id 请到http://www.weathercn.com查
'cacheTime' => 2*60*60, //更新缓存是时间 2个小时
'cacheIndex' => 'cache/cacheWeatherIndex.htm', //放在首页的天气的缓存文件
'cacheFwrx' => 'cache/cacheWeatherFwrx.htm' //放在服务热线的天气的缓存文件
);
// 服务热线调用:
if(@$_GET['id']=="fwrx"){
$filename=$config['cacheFwrx'];
// if(缓存在两的小时前更新
if((time()-filemtime($filename))>$config['cacheTime']){
$weather=weather($config['id']);
echo $str= $weather['时间'] ."
".$weather['天气云图'].$weather['天气云图2']
."
".$weather['气温']." ".$weather['风讯'];
file_put_contents($filename,$str);
}else{
require_once($filename);
}
// index 首页调用
}elseif (@$_GET['id']=='index'){
$filename=$config['cacheIndex'];
//if(缓存在两的小时前更新
if((time()-filemtime($filename))>$config['cacheTime']){
$weather=weather($config['id']);
$str= '';
$str.= $weather['时间'] .' 南宁市24小时天气预报: '. $weather['天气']. '·'.$weather['气温'].'·'. $weather['风讯'];
echo $str.='';
file_put_contents($filename,$str);
}else {
require_once($filename);
}
}else {
echo 'You have to sent the ID';
}
/**
* 天气预报数据获取
* 编码:gb2312
*
* @author 潘春孟
* @version 0.1 2006.12.20 更新于 07.1.2
* @param int $id 城市号 南宁:59431
* @return Array weather
*/
function weather($id=59431){
$from= str_replace("\n",'',@file_get_contents("http://www.weathercn.com/forecast/24time.jsp?sta_id=$id"));
preg_match_all("/(.*?)/",$from,$element);
$element[1][10]=str_replace(',',$element[1][10]);
if(strspn('.gif',$element[1][11])){
$pic2=1;
$element[1][11]=str_replace('','',$element[1][11]);
$element[1][11]=str_replace(',',$element[1][11]);
$pic2Eliment=$element[1][11];
}
else{
$pic2=0;
$pic2Eliment='';
}
preg_match_all("//",$element[1][10],$tq);
if($pic2Eliment){
preg_match_all("//",$pic2Eliment,$tq2);
$tq[1][0].="转".$tq2[1][0];
}
$weather=array(
'时间' => trim(strip_tags($element[1][9])),
'天气云图' => trim($element[1][10]),
'天气云图2' => trim($pic2Eliment),
'气温' => trim(strip_tags($element[1][11+$pic2])),
'风讯' => trim(strip_tags($element[1][12+$pic2])),
'天气描述' => trim(strip_tags($element[1][25+$pic2])),
'地理描述' => trim(strip_tags($element[1][28+$pic2])),
'天气' => trim($tq[1][0])
);
$element028=explode('|',strip_tags($element[1][28+$pic2]));
$weather['日出']=trim($element028[0]);
$weather['日落']=trim($element028[1]);
$weather['东经']=trim($element028[2]);
$weather['北纬']=trim($element028[3]);
$weather['海拔']=trim($element028[4]);
return $weather;
}
?>
相关阅读 更多 +
排行榜 更多 +