tp3.2通过高德地图行政区域查询拿到省市区信息存入

高德地图行政区域查询拿到省市区信息存入

/**
     * 发送HTTP请求方法,目前只支持CURL发送请求
     * @param  string $url    请求URL
     * @param  array  $param  GET参数数组
     * @param  array  $data   POST的数据,GET请求时该参数无效
     * @param  string $method 请求方法GET/POST
     * @return array          响应数据
     */
    public function httpCurl($url, $param, $data = '', $method = 'GET'){
        $opts = array(
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
        );
        /* 根据请求类型设置特定参数 */
        $opts[CURLOPT_URL] = $url . '?' . http_build_query($param);
        if(strtoupper($method) == 'POST'){
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $data;

            if(is_string($data)){ //发送JSON数据
                $opts[CURLOPT_HTTPHEADER] = array(
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length: ' . strlen($data),
                );
            }
        }
        /* 初始化并执行curl请求 */
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        $data  = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        return  $data;
    }
    /**
     * Notes:
     * User: jew
     * DateTime: 2022/8/4 17:27
     * describe: 通过高德地图行政区域查询拿到省市区信息排序并返回信息
     * @param $keywords
     * @return mixed
     */
    protected function getAddress($keywords){
        $url = 'https://restapi.amap.com/v3/config/district';
        $getarr = array(
            'key'=>'xxxxxxxxxxxx',//写你自己申请的key
            'keywords'=>$keywords,
            'subdistrict'=>'3'//省市区三级
        );
        $result = $this->httpCurl($url,$getarr);
        $data = json_decode($result,true);//转json格式
        $leveldata = $data['districts']['0']['districts'];
        $sort = array_column($leveldata, 'adcode');
        // 先按照sort字段升序,再按照name字段降序
        array_multisort($sort, SORT_ASC, $leveldata);
        return $leveldata;
    }
    /**
     * Notes:
     * User: jew
     * DateTime: 2022/8/4 17:25
     * describe: 高德地图行政区域查询拿到省市区信息存入
     */
    public function gaodeAdd()
    {
        $result = $this->getAddress('中国');
        foreach ($result as $key=>$value) {
            //省级数据添加
            $province_add = array(
                'citycode'=>$value['citycode'],
                'adcode'=>$value['adcode'],
                'name'=>$value['name'],
                'center'=>$value['center'],
                'level'=>$value['level'],
                'parent_id'=>'100000',//中国
            );
            @M('districts_gaode')->add($province_add);

            //根据行政区划码升序 并添加市级数据
            $city = $value['districts'];
            $sort = array_column($city, 'adcode');
            array_multisort($sort, SORT_ASC, $city);
            foreach ($city as $key2=>$value2){
                $city_add = array(
                    'citycode'=>$value2['citycode'],
                    'adcode'=>$value2['adcode'],
                    'name'=>$value2['name'],
                    'center'=>$value2['center'],
                    'level'=>$value2['level'],
                    'parent_id'=>$value['adcode'],//对应的省级行政区划码
                );
                @M('districts_gaode')->add($city_add);

                //根据行政区划码升序 并添加县/区/县级市 级数据
                $district = $value2['districts'];
                $sort_dis = array_column($district, 'adcode');
                array_multisort($sort_dis, SORT_ASC, $district);
                foreach ($district as $key3=>$value3){
                    $district_add = array(
                        'citycode'=>$value3['citycode'],
                        'adcode'=>$value3['adcode'],
                        'name'=>$value3['name'],
                        'center'=>$value3['center'],
                        'level'=>$value3['level'],
                        'parent_id'=>$value2['adcode'],//对应的市级行政区划码
                    );
                    @M('districts_gaode')->add($district_add);
                    unset($district_add);
                }
                unset($city_add);
                unset($district);
                unset($sort_dis);
            }

            unset($province_add);
            unset($city);
            unset($sort);
        }

        echo 'ok';
    }
表结构
sb_districts_gaode

CREATE TABLE `sb_districts_gaode` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `citycode` varchar(255) DEFAULT NULL,
  `adcode` varchar(255) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `center` varchar(255) DEFAULT NULL,
  `level` varchar(255) DEFAULT NULL,
  `parent_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3660 DEFAULT CHARSET=utf8;

2022-08-04 拿到的.sql文件
https://www.aliyundrive.com/s/nMg1WoXDXBa

你可能感兴趣的:(tp3.2通过高德地图行政区域查询拿到省市区信息存入)