记录: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、street
public 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);
}
}
}
}
}
}
}
文章评论