function getDistance($location1,$location2){ $url = "https://apis.map.qq.com/ws/distance/v1/"; $data = array(); $data['mode']='driving'; $data['from']= $location1['lat'].','.$location1['lng']; $data['to']= $location2['lat'].','.$location2['lng']; $data['key']='AUQBZ-2OTRX-5EY4L-73XNO-IIPLO-76FWE'; $result = curl($url,$data); $res = json_decode($result,true); $data = array(); $data['status'] = $res['status']; $data['distance'] = $res['result']['elements'][0]['distance']; $data['msg'] = $res['message']; return $data; }
/** * curl请求指定url (get) * @param $url * @param array $data * @return mixed */ function curl($url, $data = []) { // 处理get数据 if (!empty($data)) { $url = $url . '?' . http_build_query($data); } $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。 $result = curl_exec($curl); curl_close($curl); return $result; }