key; } /** * @param mixed $key */ public function setKey($key): void { $this->key = $key; } public function getLocation($longitude,$latitude){ $loc="$latitude,$longitude"; $res=self::client()->get('https://apis.map.qq.com/ws/geocoder/v1/',[ 'query'=>$this->query([ 'location'=>$loc, ]), ]); $res=$this->result($res); return [ 'province'=>$res['ad_info']['province'], 'city'=>$res['ad_info']['city'], 'address'=>$res['address'], ]; } public static function client(){ static $client; if(!$client){ $client=new Client(); } return $client; } private function query(array $query){ return array_merge($query,[ 'key'=>$this->getKey(), ]); } private function result(ResponseInterface $response){ $data=$response->getBody()->getContents(); if(empty($data)){ throw_user('调用tm返回值不存在'); } $json=json_decode($data,true); if(!$json){ throw_user('调用tm解析结果错误'); } if($json['status']>0){ throw_user("调用tm解析结果错误:{$json['message']}"); } return $json['result']; } }