Import.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Area\Src;
  3. use Dever;
  4. class Import
  5. {
  6. private $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/';
  7. /**
  8. * 获取国家统计局最新的地区数据
  9. *
  10. * @return mixed
  11. */
  12. public function load()
  13. {
  14. $url = $this->url . 'index.html';
  15. $html = Dever::curl($url);
  16. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  17. preg_match_all('/<td><a href=\'(.*?)\'>(.*?)<br\/><\/a><\/td>/i', $html, $result);
  18. # 获取省份
  19. $this->getProvince($result);
  20. return 1;
  21. }
  22. public function getProvince($result)
  23. {
  24. $update = array();
  25. if (isset($result[1]) && isset($result[2])) {
  26. foreach ($result[2] as $k => $v) {
  27. $update['id'] = $this->id(trim($result[1][$k], '.html'));
  28. $update['name'] = $v;
  29. $id = Dever::upinto('area/province', $update, $update);
  30. # 获取城市
  31. $this->getCity($id, $update['name'], $result[1][$k]);
  32. }
  33. }
  34. }
  35. public function getCity($province, $province_name, $link)
  36. {
  37. $url = $this->url . $link;
  38. $html = Dever::curl($url);
  39. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  40. preg_match_all('/<tr class=\'citytr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
  41. $update = array();
  42. if (isset($result[3]) && isset($result[4])) {
  43. foreach ($result[4] as $k => $v) {
  44. if ($v == '市辖区') {
  45. $v = $province_name;
  46. }
  47. $update['id'] = $this->id($result[2][$k]);
  48. $update['name'] = $v;
  49. $update['province_id'] = $province;
  50. $id = Dever::upinto('area/city', $update, $update);
  51. # 获取县区
  52. $this->getCounty($id, $result[3][$k]);
  53. }
  54. }
  55. }
  56. public function getCounty($city, $link)
  57. {
  58. $url = $this->url . $link;
  59. $html = Dever::curl($url);
  60. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  61. preg_match_all('/<tr class=\'countytr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
  62. $update = array();
  63. if (isset($result[3]) && isset($result[4])) {
  64. foreach ($result[4] as $k => $v) {
  65. $update['id'] = $this->id($result[2][$k]);
  66. $update['name'] = $v;
  67. $update['city_id'] = $city;
  68. Dever::upinto('area/county', $update, $update);
  69. }
  70. }
  71. }
  72. public function id($id)
  73. {
  74. return Dever::load('area/api')->code($id);
  75. }
  76. }