LbsService.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Services\Vendor;
  3. use App\Services\Service;
  4. use GuzzleHttp\Client;
  5. class LbsService extends Service
  6. {
  7. private $httpClient;
  8. private $key;
  9. public function __construct()
  10. {
  11. $this->key = config('services.lbs.key');
  12. $this->httpClient = new Client();
  13. }
  14. /**
  15. * 行政区域查询
  16. * @param array $parameters
  17. * @return mixed
  18. * @throws \GuzzleHttp\Exception\GuzzleException
  19. */
  20. public function district(array $parameters)
  21. {
  22. $parameters['key'] = $this->key;
  23. $url = 'https://restapi.amap.com/v3/config/district';
  24. $response = $this->httpClient->request('GET', $url, [
  25. 'query' => $parameters,
  26. ]);
  27. return json_decode($response->getBody(), true);
  28. }
  29. /**
  30. * IP定位
  31. * @param array $parameters
  32. * @return mixed
  33. * @throws \GuzzleHttp\Exception\GuzzleException
  34. */
  35. public function ip(array $parameters)
  36. {
  37. $parameters['key'] = $this->key;
  38. $url = 'https://restapi.amap.com/v3/ip';
  39. $response = $this->httpClient->request('GET', $url, [
  40. 'query' => $parameters,
  41. ]);
  42. return json_decode($response->getBody(), true);
  43. }
  44. /**
  45. * 地理编码
  46. * @param array $parameters
  47. * @return mixed
  48. * @throws \GuzzleHttp\Exception\GuzzleException
  49. */
  50. public function geocode(array $parameters)
  51. {
  52. $parameters['key'] = $this->key;
  53. $url = 'https://restapi.amap.com/v3/geocode/geo';
  54. $response = $this->httpClient->request('GET', $url, [
  55. 'query' => $parameters,
  56. ]);
  57. return json_decode($response->getBody(), true);
  58. }
  59. /**
  60. * 逆地理编码
  61. * @param array $parameters
  62. * @return mixed
  63. * @throws \GuzzleHttp\Exception\GuzzleException
  64. */
  65. public function reGeocode(array $parameters)
  66. {
  67. $parameters['key'] = $this->key;
  68. $url = 'https://restapi.amap.com/v3/geocode/regeo';
  69. $response = $this->httpClient->request('GET', $url, [
  70. 'query' => $parameters,
  71. ]);
  72. return json_decode($response->getBody(), true);
  73. }
  74. }