sendRequest($url, 'get', ['ip' => $ip]); return $data; } /** * 根据经纬度获取地理位置信息 * @param $lat * @param $lng * @return mixed */ function getLocationAddress($lat, $lng) { $url = 'https://apis.map.qq.com/ws/geocoder/v1/'; $data = $this->sendRequest($url, 'get', ['location' => "{$lat},{$lng}"]); return $data; } /** * @param $url * @param string $type 请求方式 * @param string $data 数据 数组格式 * @return mixed */ protected function sendRequest($url, $type = 'get', $data = []) { $data['key'] = $this->key; $ch = curl_init(); if ($type == 'get' && $data) { $url = $url . '?' . http_build_query($data); } curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $head = array('User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'); curl_setopt($ch, CURLOPT_HTTPHEADER, $head); if ($type == 'post') { curl_setopt($ch, CURLOPT_POST, TRUE);//post请求 $data = json_encode($data, JSON_UNESCAPED_UNICODE); curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置传递的参数 } $request = curl_exec($ch); curl_close($ch); $request = json_decode($request, true); return $request; } }