url . 'index.html'; $html = Dever::curl($url); $html = mb_convert_encoding($html, "UTF-8", "GB2312"); preg_match_all('/(.*?)<\/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('/(.*?)<\/a><\/td>(.*?)<\/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('/(.*?)<\/a><\/td>(.*?)<\/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('/(.*?)<\/a><\/td>(.*?)<\/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('/(.*?)<\/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']); } } }