-
- /**
- * php weather forecast code
- * by bbs.it-home.org
- */
- if(!isset($_GET['q'])){
- //Query the location based on IP
- $p = file_get_contents("http: //bbs.it-home.org/ip/?q={$_SERVER['HTTP_X_FORWARDED_FOR']}");
- }
- else{
- $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" => "Macau",
- "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");
- ?>
Copy code
Articles you may be interested in:
PHP calls yahoo sina api weather forecast implementation code
Example code of php calling google weather api
Google API php code to get current weather information
|