| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- <?php
- namespace app\utils;
- class AddressGeo
- {
- // 阿里云使用地理编码-百度版
- // 文档 https://market.aliyun.com/apimarket/detail/cmapi00067007
- // 测试code: 505645dec3e747e3b294acbf5053c3a4 Todo 删掉
- public static function AliCloud($address, $appcode)
- {
- try {
- if (empty($address) || empty($appcode)) {
- throw new \Exception('Error: address or appcode is empty');
- }
- $api = 'https://lhjwddzzh.market.alicloudapi.com/geocode/baidu/geo/query';
- $res = http_post($api, [
- 'body' => http_build_query([
- 'address' => $address,
- ]),
- 'headers' => [
- 'Content-Type' => 'application/x-www-form-urlencoded',
- 'Authorization' => 'APPCODE ' . $appcode,
- ],
- ]);
- if ($res->getStatusCode() != 200) {
- throw new \Exception('Error: ' . $res->getStatusCode() . ' ' . $res->getReasonPhrase());
- }
- $data = json_decode((string)$res->getBody(), true);
- if ($data['code'] == 200) {
- return [
- 'lng' => $data['data']['location']['lng'],
- 'lat' => $data['data']['location']['lat'],
- ];
- }
- throw new \Exception('Error: ' . $data['code'] . ' ' . $data['msg']);
- } catch (\Throwable $e) {
- return [
- 'lng' => 0,
- 'lat' => 0,
- ];
- }
- }
- }
|