一聚教程网:一个值得你收藏的教程网站

热门教程

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

时间:2022-06-24 18:28:19 编辑:袖梨 来源:一聚教程网

    

 代码如下 复制代码

作者:    freemouse
     主页:    www.cnphp.info
     接口demo: http://www.cnphp.info/tianqi
     用法: 可以直接调用上面的地址会显示当地的天气信息,也可以这样用
    http://www.cnphp.info/tianqi/?q=江苏省南京市
    **/

    $p = $_get['q'];
    $k = 0;
    $encoding = mb_detect_encoding($p);
    if($encoding != "utf-8"){
        $p = mb_convert_encoding($p,"utf-8","gbk");
    }
    $p_arr = array(
       "01" => "北京",
      "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");

热门栏目