php 天氣預報代碼 採集自中央氣象台範圍覆蓋全國

來源:互聯網
上載者:User

    

 代碼如下 複製代碼

作者:    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");

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.