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循环省份,分析城市,再循环分析地区,最后获得完整的树结构

转载于:https://www.cnblogs.com/roscoe/archive/2013/05/09/3069212.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值