Import.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Area\Src;
  3. set_time_limit(0);
  4. use Dever;
  5. class Import
  6. {
  7. private $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2020/';
  8. /**
  9. * 获取国家统计局最新的地区数据
  10. *
  11. * @return mixed
  12. */
  13. public function load()
  14. {
  15. $url = $this->url . 'index.html';
  16. $html = Dever::curl($url);
  17. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  18. preg_match_all('/<td><a href=\'(.*?)\'>(.*?)<br\/><\/a><\/td>/i', $html, $result);
  19. Dever::config('base')->hook = true;
  20. # 获取省份
  21. $this->getProvince($result);
  22. return 1;
  23. }
  24. public function getProvince($result)
  25. {
  26. $update = array();
  27. if (isset($result[1]) && isset($result[2])) {
  28. foreach ($result[2] as $k => $v) {
  29. $update['id'] = $this->id(trim($result[1][$k], '.html'));
  30. $update['name'] = strip_tags($v);
  31. $this->pinyin($update);
  32. $id = Dever::upinto('area/province', $update, $update);
  33. # 获取城市
  34. $this->getCity($id, $update['name'], $result[1][$k]);
  35. }
  36. }
  37. }
  38. public function getCity($province, $province_name, $link)
  39. {
  40. $url = $this->url . $link;
  41. $html = Dever::curl($url);
  42. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  43. preg_match_all('/<tr class=\'citytr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
  44. $update = array();
  45. if (isset($result[3]) && isset($result[4])) {
  46. foreach ($result[4] as $k => $v) {
  47. $v = strip_tags($v);
  48. if ($v == '市辖区') {
  49. $v = $province_name;
  50. }
  51. $update['id'] = $this->id($result[2][$k]);
  52. $update['name'] = $v;
  53. $update['province_id'] = $province;
  54. $this->pinyin($update);
  55. $id = Dever::upinto('area/city', $update, $update);
  56. # 获取县区
  57. $this->getCounty($province, $id, $result[3][$k]);
  58. }
  59. }
  60. }
  61. public function getCounty($province, $city, $link)
  62. {
  63. $url = $this->url . $link;
  64. $temp = explode('/', $link);
  65. $link = $temp[0];
  66. $html = Dever::curl($url);
  67. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  68. preg_match_all('/<tr class=\'countytr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
  69. $update = array();
  70. if (isset($result[3]) && isset($result[4])) {
  71. foreach ($result[4] as $k => $v) {
  72. $update['id'] = $this->id($result[2][$k]);
  73. $update['name'] = strip_tags($v);
  74. $update['city_id'] = $city;
  75. $update['province_id'] = $province;
  76. $update['area'] = $province . ',' . $city;
  77. $num = substr($update['id'], 4);
  78. # type = 1城区 2郊区 3县城 4经济技术开发 5县级市
  79. if ($v == '门头沟区') {
  80. $update['type'] = 2;
  81. $update['level'] = 2;
  82. } elseif ($num <= 10) {
  83. $update['type'] = 1;
  84. $update['level'] = 1;
  85. } elseif ($num > 10 && $num <= 20) {
  86. $update['type'] = 2;
  87. $update['level'] = 2;
  88. } elseif ($num > 20 && $num <= 70) {
  89. $update['type'] = 3;
  90. $update['level'] = 3;
  91. } elseif ($num > 70 && $num <= 80) {
  92. $update['type'] = 4;
  93. $update['level'] = 2;
  94. } elseif ($num >= 80) {
  95. $update['type'] = 5;
  96. $update['level'] = 2;
  97. }
  98. $this->pinyin($update);
  99. $id = Dever::upinto('area/county', $update, $update);
  100. # 获取街道
  101. $this->getTown($province, $city, $id, $link . '/' . $result[3][$k]);
  102. }
  103. }
  104. }
  105. public function getTown($province, $city, $county, $link)
  106. {
  107. $url = $this->url . $link;
  108. $temp = explode('/', $link);
  109. $link = $temp[0] . '/' . $temp[1];
  110. $html = Dever::curl($url);
  111. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  112. preg_match_all('/<tr class=\'towntr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
  113. $update = array();
  114. if (isset($result[3]) && isset($result[4])) {
  115. foreach ($result[4] as $k => $v) {
  116. $update['id'] = $this->id($result[2][$k], 9);
  117. $update['name'] = strip_tags($v);
  118. $update['county_id'] = $county;
  119. $update['city_id'] = $city;
  120. $update['province_id'] = $province;
  121. $update['area'] = $province . ',' . $city . ',' . $county;
  122. $this->pinyin($update);
  123. $id = Dever::upinto('area/town', $update, $update);
  124. # 获取社区
  125. //$this->getVillage($province, $city, $county, $id, $link . '/' . $result[3][$k]);
  126. }
  127. }
  128. }
  129. public function getVillage($province, $city, $county, $town, $link)
  130. {
  131. $url = $this->url . $link;
  132. $html = Dever::curl($url);
  133. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  134. preg_match_all('/<tr class=\'villagetr\'><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><\/tr>/i', $html, $result);
  135. $update = array();
  136. if (isset($result[1]) && isset($result[2]) && isset($result[3])) {
  137. foreach ($result[3] as $k => $v) {
  138. $update['id'] = $this->id($result[1][$k], 12);
  139. $update['code'] = $result[2][$k];
  140. $update['name'] = strip_tags($v);
  141. $update['town_id'] = $town;
  142. $update['county_id'] = $county;
  143. $update['city_id'] = $city;
  144. $update['province_id'] = $province;
  145. $update['area'] = $province . ',' . $city . ',' . $county . ',' . $town;
  146. $this->pinyin($update);
  147. Dever::upinto('area/village', $update, $update);
  148. }
  149. }
  150. }
  151. public function id($id, $len = 6)
  152. {
  153. return Dever::load('area/api')->code($id, $len);
  154. }
  155. public function pinyin(&$update)
  156. {
  157. if (Dever::import('pinyin') && $update['name']) {
  158. $update['pinyin'] = Dever::getPinyin($update['name']);
  159. $update['pinyin_first'] = Dever::getPinyinFirst($update['name']);
  160. }
  161. }
  162. }