php 获取行政区域

news/2024/11/29 4:05:30/
记录:php获取四级行政区域,拉取高德接口数据

API接口文档:
https://lbs.amap.com/api/webservice/guide/api/district
获取API

<?php
/*** 服务器接口类*/
namespace queryAreaInfo;class queryArea{//发送private function sendData($dataStr){// $url = 'http://restapi.amap.com/v3/config/district?';	//获取url$url = 'https://restapi.amap.com/v3/config/district?parameters';$ch = curl_init();curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS,$dataStr);curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);curl_setopt($ch, CURLOPT_TIMEOUT, 30);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);curl_setopt($ch, CURLOPT_URL,$url);//设置获取的信息以文件流的形式返回,而不是直接输出。  curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//返回值赋值给$result$result = curl_exec($ch);//调试整套请求代码//释放      curl_close($ch);$result = json_decode($result,true);return $result;}//获取地区信息public function getAreaInfo($data){$dataStr = implode('&',$data);return $this->sendData($dataStr);}
}

调用API并数据保存

use queryAreaInfo\queryArea;
use fast\Pinyin;
    //获取区域信息,调用高德地图接口public function getSonAreaInfos($provid){//获取key$key = 'web服务类型API';//组合接口数据$data=['keywords='.$provid.'','subdistrict=6','key='.$key.'',];//获取验证字符串$queryArea=new queryArea();$result=$queryArea->getAreaInfo($data);return $result;}//获取区域,四个等级 province、city、district、streetpublic function get_area_info(){$Pinyin = new Pinyin();$levalArr = ['province'=>1,'city'=>2,'district'=>3,'street'=>4,];// $provinceArr = ["北京市","天津市","河北省","山西省","内蒙古自治区","辽宁省","吉林省","黑龙江省","上海市","江苏省","浙江省","安徽省","福建省","江西省","山东省","河南省","湖北省","湖南省","广东省","广西壮族自治区","海南省","重庆市","四川省","贵州省","云南省","西藏自治区","陕西省","甘肃省","青海省","宁夏回族自治区","新疆维吾尔自治区","台湾省","香港特别行政区","澳门特别行政区"];$provinceArr = ["北京市"];foreach($provinceArr as $province){$provinceInfoArr = $this->getSonAreaInfos($province)['districts'];// echo '<pre>';print_r($provinceInfoArr);exit;foreach ($provinceInfoArr as $key => $provinceInfo) {if(!Db::name('area1')->where(['pid'=>0,'name'=>$provinceInfo['name']])->find()){// 省$provinceData = ['pid'=>0,'shortname'=>'','name'=>$provinceInfo['name'],'mergename'=>$provinceInfo['name'],'level'=>$levalArr[$provinceInfo['level']],'pinyin'=>$Pinyin->get($provinceInfo['name'],$onlyfirst = false, $delimiter = '', $ucfirst = false),'code'=>$provinceInfo['citycode'],'zip'=>'','first'=>explode('|',$Pinyin->get($provinceInfo['name'],$onlyfirst = true, $delimiter = '|', $ucfirst = true))[0],'lng'=>explode(',',$provinceInfo['center'])[0],'lat'=>explode(',',$provinceInfo['center'])[1],];echo '<pre>';print_r($provinceData);$provinceId = Db::name('area1')->insertGetId($provinceData);}else{$provinceId = Db::name('area1')->where(['pid'=>0,'name'=>$provinceInfo['name']])->value('id');}foreach ($provinceInfo['districts'] as $key => $cityInfo) {if(!Db::name('area1')->where(['pid'=>$provinceId,'name'=>$cityInfo['name']])->find()){// 市$cityData = ['pid'=>$provinceId,'shortname'=>'','name'=>$cityInfo['name'],'mergename'=>$provinceInfo['name'].','.$cityInfo['name'],'level'=>$levalArr[$cityInfo['level']],'pinyin'=>$Pinyin->get($cityInfo['name'],$onlyfirst = false, $delimiter = '', $ucfirst = false),'code'=>$cityInfo['citycode'],'zip'=>'','first'=>explode('|',$Pinyin->get($cityInfo['name'],$onlyfirst = true, $delimiter = '|', $ucfirst = true))[0],'lng'=>explode(',',$cityInfo['center'])[0],'lat'=>explode(',',$cityInfo['center'])[1],];echo '<pre>';print_r($cityData);$cityId = Db::name('area1')->insertGetId($cityData);}else{$cityId = Db::name('area1')->where(['pid'=>$provinceId,'name'=>$cityInfo['name']])->value('id');}foreach ($cityInfo['districts'] as $key => $districtInfo) {if(!Db::name('area1')->where(['pid'=>$cityId,'name'=>$districtInfo['name']])->find()){// 县区$districtData = ['pid'=>$cityId,'shortname'=>'','name'=>$districtInfo['name'],'mergename'=>$provinceInfo['name'].','.$cityInfo['name'].','.$districtInfo['name'],'level'=>$levalArr[$districtInfo['level']],'pinyin'=>$Pinyin->get($districtInfo['name'],$onlyfirst = false, $delimiter = '', $ucfirst = false),'code'=>$districtInfo['citycode'],'zip'=>'','first'=>explode('|',$Pinyin->get($districtInfo['name'],$onlyfirst = true, $delimiter = '|', $ucfirst = true))[0],'lng'=>explode(',',$districtInfo['center'])[0],'lat'=>explode(',',$districtInfo['center'])[1],];echo '<pre>';print_r($districtData);$districtId = Db::name('area1')->insertGetId($districtData);}else{$districtId = Db::name('area1')->where(['pid'=>$cityId,'name'=>$districtInfo['name']])->value('id');}foreach ($districtInfo['districts'] as $key => $streetInfo) {if(!Db::name('area1')->where(['pid'=>$districtId,'name'=>$streetInfo['name']])->find()){// 街道$streetData = ['pid'=>$districtId,'shortname'=>'','name'=>$streetInfo['name'],'mergename'=>$provinceInfo['name'].','.$cityInfo['name'].','.$districtInfo['name'].','.$streetInfo['name'],'level'=>$levalArr[$streetInfo['level']],'pinyin'=>$Pinyin->get($streetInfo['name'],$onlyfirst = false, $delimiter = '', $ucfirst = false),'code'=>$streetInfo['citycode'],'zip'=>'','first'=>explode('|',$Pinyin->get($streetInfo['name'],$onlyfirst = true, $delimiter = '|', $ucfirst = true))[0],'lng'=>explode(',',$streetInfo['center'])[0],'lat'=>explode(',',$streetInfo['center'])[1],];echo '<pre>';print_r($streetData);$streetId = Db::name('area1')->insertGetId($streetData);}}}}}}}

http://www.ppmy.cn/news/1418514.html

相关文章

Java 面试宝典:你知道多少种解决 hash 冲突的方法?

大家好&#xff0c;我是大明哥&#xff0c;一个专注「死磕 Java」系列创作的硬核程序员。 本文已收录到我的技术网站&#xff1a;https://www.skjava.com。有全网最优质的系列文章、Java 全栈技术文档以及大厂完整面经 回答 在使用 hash 表时&#xff0c; hash 冲突是一个非常…

SSM整合----第一个SSM项目

文章目录 前言一、使用步骤1.引入库2.建表3 项目结构4 web.xml的配置5 配置数据源6 SpringMVC配置7 配置MyBatis Mapper8 书写控制类 总结 前言 提示&#xff1a;这里可以添加本文要记录的大概内容&#xff1a; SSM整合是指Spring、SpringMVC和MyBatis这三个框架的整合使用。…

顺序表实战——基于顺序表的通讯录

前言&#xff1a;本篇文章主要是利用顺序表作为底层&#xff0c; 实现一个通讯录。偏向于应用&#xff0c; 对于已经学习过c的友友们可能没有难度了已经。没有学习过c的友友&#xff0c; 如果顺序表不会写&#xff0c; 或者说没有自己实现过&#xff0c; 请移步学习顺序表相关内…

【论文阅读】Digging Into Self-Supervised Monocular Depth Estimation

论文&#xff1a;https://arxiv.org/pdf/1806.01260.pdf 代码&#xff1a;https://github.com/nianticlabs/monodepth2 Q: 这篇论文试图解决什么问题&#xff1f; A: 这篇论文试图解决的问题是如何提高仅使用单目图像进行深度估计的性能。具体来说&#xff0c;它关注的是如何…

linux查看硬盘空间使用情况

df &#xff08;1&#xff09;查看磁盘空间的占用情况 -h是给大小带上单位 df -h 总空间不一定等于已用未用&#xff0c;系统可能留出来一点空间另做他用 &#xff08;2&#xff09;查看INode的使用情况 df -idu du命令比df命令复杂一点&#xff0c;是查看文件和目录占用的…

【设计模式学习】单例模式和工厂模式

꒵˂͈꒱ write in front ꒰˃͈꒵˂͈꒱ ʕ̯•͡˔•̯᷅ʔ大家好&#xff0c;我是xiaoxie.希望你看完之后,有不足之处请多多谅解&#xff0c;让我们一起共同进步૮₍❀ᴗ͈ . ᴗ͈ აxiaoxieʕ̯•͡˔•̯᷅ʔ—CSDN博客 本文由xiaoxieʕ̯•͡˔•̯᷅ʔ 原创 CSDN 如需转…

步骤大全:网站建设3个基本流程详解

一.领取一个免费域名和SSL证书&#xff0c;和CDN 1.打开网站链接&#xff1a;https://www.rainyun.com/z22_ 2.在网站主页上&#xff0c;您会看到一个"登陆/注册"的选项。 3.点击"登陆/注册"&#xff0c;然后选择"微信登录"选项。 4.使用您的…

探索量子计算:打开未来技术的大门

在科技领域&#xff0c;每一次技术革命都能开启新的可能性&#xff0c;推动人类社会进入一个新的时代。当前&#xff0c;量子计算作为一种前沿技术&#xff0c;正引领着下一轮科技革命的浪潮。本文将深入探索量子计算的奥秘&#xff0c;解析其工作原理&#xff0c;并通过一个简…