PHP CURL根据详细地址获取腾讯地图经纬度
1 <?php 2 3 4 $address = "广东省广州市天河区"; 5 $point = getPoint($address); 6 7 var_dump($point);//输出经纬度 8 9 10 /** 11 * 【根据详细地址获取经纬度】 12 * 20170920 13 * 14 * @param $address 15 * @return array 16 */ 17 function getPoint($address){ 18 $url = "http://apis.map.qq.com/jsapi?qt=geoc&addr={$address}}&key=6KLBZ-EW7CV-BVFPV-UUFU2-6STGE-G7BI7&output=jsonp&pf=jsapi&ref=jsapi&cb=qq.maps._svcb3.geocoder0"; 19 $ch = curl_init($url); 20 21 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//转为字符串,而不是直接输出 22 23 $wetContent = curl_exec($ch); 24 25 26 $data = iconv("GB18030", "UTF-8//IGNORE", $wetContent); 27 28 $match = '/"pointx":"([\s\S]*?)",\s*?"pointy":"([\s\S]*?)"/'; 29 30 if(preg_match($match,$data,$rst)){ 31 $arr = [ 32 'longitude' => $rst[1], 33 'latitude' => $rst[2], 34 ]; 35 }else{ 36 $arr = [ 37 'longitude' => '', 38 'latitude' => '', 39 ]; 40 } 41 42 curl_close($ch); 43 return $arr; 44 45 46 }
生命不息,学习不止