|
@@ -0,0 +1,90 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Area\Src;
|
|
|
+
|
|
|
+use Dever;
|
|
|
+
|
|
|
+class Import
|
|
|
+{
|
|
|
+ private $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/';
|
|
|
+
|
|
|
+ * 获取国家统计局最新的地区数据
|
|
|
+ *
|
|
|
+ * @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);
|
|
|
+
|
|
|
+
|
|
|
+ $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['name'] = $v;
|
|
|
+ $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) {
|
|
|
+ if ($v == '市辖区') {
|
|
|
+ $v = $province_name;
|
|
|
+ }
|
|
|
+ $update['name'] = $v;
|
|
|
+ $update['province_id'] = $province;
|
|
|
+ $id = Dever::upinto('area/city', $update, $update);
|
|
|
+
|
|
|
+
|
|
|
+ $this->getCounty($id, $result[3][$k]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getCounty($city, $link)
|
|
|
+ {
|
|
|
+ $url = $this->url . $link;
|
|
|
+
|
|
|
+ $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['name'] = $v;
|
|
|
+ $update['city_id'] = $city;
|
|
|
+ Dever::upinto('area/county', $update, $update);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|