composer require guzzlehttp/guzzle使用guzzle来发起GET请求 引入类
注:如果出现报错,请参考这篇文档:https://www.cnblogs.com/houss/p/10992813.html
主要代码:all();
$place=$data['place'];
//高德地图网址 https://console.amap.com/dev/index 应用管理>我的应用申请的key
$key="ecfbe7785caa972e7c13da2104d62fcb";
//将高德地图地图的api从config配置中读取出来
//config('gode.gaodeapi');
//请求的地址
$api="https://restapi.amap.com/v3/geocode/geo?address=$place&output=json&key=$key";
//申请一个请求类并设置过期时间
$client=new Client(['timeout'=>5]);
//发起请求
$response=$client->get($api);
$body=(string)
$response->getBody();
//转换成数组
$arr=json_decode($body,true);
$location=explode(',',$arr['geocodes'][0]['location']);
$res=[
//经度
'longitude'=>$location[0],
//纬度
'latitude'=>$location[1]
];
//输出经纬度
print_r($res);
}
}
效果:



