123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace Area\Src;
- set_time_limit(0);
- use Dever;
- class Import
- {
- private $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2020/';
- /**
- * 获取国家统计局最新的地区数据
- *
- * @return mixed
- */
- public function load()
- {
- $url = $this->url . 'index.html';
-
- $html = Dever::curl($url);
- $html = mb_convert_encoding($html, "UTF-8", "GB2312");
- preg_match_all('/<td><a href=\'(.*?)\'>(.*?)<br\/><\/a><\/td>/i', $html, $result);
- Dever::config('base')->hook = true;
- # 获取省份
- $this->getProvince($result);
- return 1;
- }
- public function getProvince($result)
- {
- $update = array();
- if (isset($result[1]) && isset($result[2])) {
- foreach ($result[2] as $k => $v) {
- $update['id'] = $this->id(trim($result[1][$k], '.html'));
- $update['name'] = strip_tags($v);
- $this->pinyin($update);
- $id = Dever::upinto('area/province', $update, $update);
- # 获取城市
- $this->getCity($id, $update['name'], $result[1][$k]);
- }
- }
- }
- public function getCity($province, $province_name, $link)
- {
- $url = $this->url . $link;
-
- $html = Dever::curl($url);
- $html = mb_convert_encoding($html, "UTF-8", "GB2312");
- preg_match_all('/<tr class=\'citytr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
- $update = array();
- if (isset($result[3]) && isset($result[4])) {
- foreach ($result[4] as $k => $v) {
- $v = strip_tags($v);
- if ($v == '市辖区') {
- $v = $province_name;
- }
- $update['id'] = $this->id($result[2][$k]);
- $update['name'] = $v;
- $update['province_id'] = $province;
- $this->pinyin($update);
- $id = Dever::upinto('area/city', $update, $update);
- # 获取县区
- $this->getCounty($province, $id, $result[3][$k]);
- }
- }
- }
- public function getCounty($province, $city, $link)
- {
- $url = $this->url . $link;
- $temp = explode('/', $link);
- $link = $temp[0];
-
- $html = Dever::curl($url);
- $html = mb_convert_encoding($html, "UTF-8", "GB2312");
- preg_match_all('/<tr class=\'countytr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
- $update = array();
- if (isset($result[3]) && isset($result[4])) {
- foreach ($result[4] as $k => $v) {
- $update['id'] = $this->id($result[2][$k]);
- $update['name'] = strip_tags($v);
- $update['city_id'] = $city;
- $update['province_id'] = $province;
- $update['area'] = $province . ',' . $city;
- $num = substr($update['id'], 4);
- # type = 1城区 2郊区 3县城 4经济技术开发 5县级市
- if ($v == '门头沟区') {
- $update['type'] = 2;
- $update['level'] = 2;
- } elseif ($num <= 10) {
- $update['type'] = 1;
- $update['level'] = 1;
- } elseif ($num > 10 && $num <= 20) {
- $update['type'] = 2;
- $update['level'] = 2;
- } elseif ($num > 20 && $num <= 70) {
- $update['type'] = 3;
- $update['level'] = 3;
- } elseif ($num > 70 && $num <= 80) {
- $update['type'] = 4;
- $update['level'] = 2;
- } elseif ($num >= 80) {
- $update['type'] = 5;
- $update['level'] = 2;
- }
- $this->pinyin($update);
- $id = Dever::upinto('area/county', $update, $update);
- # 获取街道
- $this->getTown($province, $city, $id, $link . '/' . $result[3][$k]);
- }
- }
- }
- public function getTown($province, $city, $county, $link)
- {
- $url = $this->url . $link;
- $temp = explode('/', $link);
- $link = $temp[0] . '/' . $temp[1];
-
- $html = Dever::curl($url);
- $html = mb_convert_encoding($html, "UTF-8", "GB2312");
- preg_match_all('/<tr class=\'towntr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
- $update = array();
- if (isset($result[3]) && isset($result[4])) {
- foreach ($result[4] as $k => $v) {
- $update['id'] = $this->id($result[2][$k], 9);
- $update['name'] = strip_tags($v);
- $update['county_id'] = $county;
- $update['city_id'] = $city;
- $update['province_id'] = $province;
- $update['area'] = $province . ',' . $city . ',' . $county;
- $this->pinyin($update);
- $id = Dever::upinto('area/town', $update, $update);
- # 获取社区
- //$this->getVillage($province, $city, $county, $id, $link . '/' . $result[3][$k]);
- }
- }
- }
- public function getVillage($province, $city, $county, $town, $link)
- {
- $url = $this->url . $link;
-
- $html = Dever::curl($url);
- $html = mb_convert_encoding($html, "UTF-8", "GB2312");
- preg_match_all('/<tr class=\'villagetr\'><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><\/tr>/i', $html, $result);
- $update = array();
- if (isset($result[1]) && isset($result[2]) && isset($result[3])) {
- foreach ($result[3] as $k => $v) {
- $update['id'] = $this->id($result[1][$k], 12);
- $update['code'] = $result[2][$k];
- $update['name'] = strip_tags($v);
- $update['town_id'] = $town;
- $update['county_id'] = $county;
- $update['city_id'] = $city;
- $update['province_id'] = $province;
- $update['area'] = $province . ',' . $city . ',' . $county . ',' . $town;
- $this->pinyin($update);
- Dever::upinto('area/village', $update, $update);
- }
- }
- }
- public function id($id, $len = 6)
- {
- return Dever::load('area/api')->code($id, $len);
- }
- public function pinyin(&$update)
- {
- if (Dever::import('pinyin') && $update['name']) {
- $update['pinyin'] = Dever::getPinyin($update['name']);
- $update['pinyin_first'] = Dever::getPinyinFirst($update['name']);
- }
- }
- }
|