The php weather forecast code is collected from the Central Meteorological Observatory covering the whole country.

Source: Internet
Author: User
Tags explode

    

The code is as follows: Copy code

Author: freemouse
Home: www.cnphp.info
Interface demo: http://www.cnphp.info/tianqi
Usage: you can directly call the above address to display the local weather information.
Http://www.cnphp.info/tianqi? Q = Nanjing city, Jiangsu province
**/

$ 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" => "Beijing ",
"02" => "Shanghai ",
"03" => "Tianjin ",
"04" => "Chongqing ",
"05" => "Heilongjiang ",
"06" => "Jilin ",
"07" => "Liaoning ",
"08" => "Inner Mongolia ",
"09" => "Hebei ",
"10" => "Shanxi ",
"11" => "Shaanxi ",
"12" => "Shandong ",
"13" => "Xinjiang ",
"14" => "Tibet ",
"15" => "Qinghai ",
"16" => "Gansu ",
"17" => "Ningxia ",
"18" => "Henan ",
"19" => "Jiangsu ",
"20" => "Hubei ",
"21" => "Zhejiang ",
"22" => "Anhui ",
"23" => "Fujian ",
"24" => "Jiangxi ",
"25" => "Hunan ",
"26" => "Guizhou ",
"27" => "Sichuan ",
"28" => "Guangdong ",
"29" => "Yunnan ",
"30" => "Guangxi ",
"31" => "Hainan ",
"32" => "Hong Kong ",
"33" => "Macao ",
"34" => "Taiwan"
);
 
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 ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.