PHP脚本获取中国天气网城市编码

 1 <?php
 2 $file = 'city.xml';
 3 $str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
 4 file_put_contents ($file, $str,FILE_APPEND);
 5 $province = "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|台湾";
 6 $array = explode (',',$province);
 7 
 8 foreach ($array as $a)
 9 {
10 $s = explode ('|',$a);
11     $str ="<p p_id = \"$s[0]\">\r\n\t<pn>$s[1]</pn>\r\n\t\t";
12     file_put_contents ($file, $str,FILE_APPEND);
13     $citys = file_get_contents ("http://m.weather.com.cn/data5/city$s[0].xml");
14     $city = explode (',',$citys);
15     foreach ($city as $c)
16     {
17     $c_ = explode (',',$c);
18         foreach ($c_ as $cl)
19         {
20         $c__ = explode ('|',$cl);
21         $str = "<c c_id=\"$c__[0]\"><cn>$c__[1]</cn>\r\n\t\t\t";
22         file_put_contents ($file, $str,FILE_APPEND);
23         $districts = file_get_contents ("http://m.weather.com.cn/data5/city$c__[0].xml");
24         $d_ = explode (',',$districts);
25         foreach ($d_ as $dl)
26             {
27             $d__ = explode ('|',$dl);
28             $tmpd = file_get_contents ("http://m.weather.com.cn/data5/city$d__[0].xml");
29             $tmpd_ = explode ('|',$tmpd);
30             $str = "\t<d d_id=\"$tmpd_[1]\">$d__[1]</d>\r\n\t\t\t";
31             
32             file_put_contents ($file, $str,FILE_APPEND);
33             }
34         $str ="</c>\r\n\t\t";
35         file_put_contents ($file, $str,FILE_APPEND);
36         }
37     }
38     $str ="</p>\r\n\r\n"; 
39     file_put_contents ($file, $str,FILE_APPEND);
40     }
41 echo 'success';
42 ?>

通过forech循环省份,分析城市,再循环分析地区,最后获得完整的树结构

posted on 2013-05-09 16:10  Roscoe  阅读(218)  评论(0编辑  收藏  举报

导航