欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php 天气预报代码 采集自*气象台范围覆盖全国

程序员文章站 2022-03-20 15:56:21
...
天气预报信息采集自*气象台,信息准确,覆盖面广,代码简单,返回是json代码,可以用于客户端调用,也可以在服务器端处理后显示,笔者的wordpress天气预报插件就是使用的这段代码,需要注意的是,这段代码会在服务器产生缓存文件,需要在当前目录中新建data文件夹,保证文件夹可写.

php 天气预报代码如下:

 "北京",  
  "02" => "上海",  
  "03" => "天津",  
  "04" => "重庆",  
  "05" => "黑龙江",  
  "06" => "吉林",  
  "07" => "辽宁",  
  "08" => "内蒙古",  
  "09" => "河北",  
  "10" => "山西",  
  "11" => "陕西",  
  "12" => "山东",  
  "13" => "*",  
  "14" => "*",  
  "15" => "青海",  
  "16" => "甘肃",  
  "17" => "宁夏",  
  "18" => "河南",  
  "19" => "江苏",  
  "20" => "湖北",  
  "21" => "浙江",  
  "22" => "安徽",  
  "23" => "福建",  
  "24" => "江西",  
  "25" => "湖南",  
  "26" => "贵州",  
  "27" => "四川",  
  "28" => "广东",  
  "29" => "云南",  
  "30" => "广西",  
  "31" => "海南",  
  "32" => "香港",  
  "33" => "澳门",  
  "34" => "*" 
);  
function find(&$item,$key,$data){  
global $k;  
if(preg_match("/$item/u",$data)){  
   $k = $key;  
}  
}  
function get_data_arr($key){  
if(!file_exists("./data/city{$key}.xml")){  
$c =file_get_contents( "http://m.weather.com.cn/data5/city{$key}.xml" );  
file_put_contents("./data/city{$key}.xml",$c);  
}  
else{  
$c = file_get_contents("./data/city{$key}.xml");  
}  
   $arr = explode(",",$c);  
   foreach($arr as $v){  
   $data = explode("|",$v);  
   $ret[$data[0]] = $data[1];  
   } 
   return $ret;  
}  
array_walk($p_arr,'find',$p);  
array_walk(get_data_arr($k),'find',$p);  
array_walk(get_data_arr($k),'find',$p);  
$ccode = get_data_arr($k);  
echo file_get_contents("http://m.weather.com.cn/data/{$ccode[$k]}.html");



本文链接:

收藏随意^^请保留教程地址.