测试经纬度信息,37.863036,113.598909。通过地图查询,所在城市为:阳泉。

<?php
class test{
public static $test_key = 'dfgfdgfh3c8ttrr1774gfdgfdgfd9rgqw1'; //这里是申请的百度appkey
$url = 'http://api.map.baidu.com/geocoder/v2/';
//37.863036
$longitude = number_format(doubleval($longitude), );
//113.598909
$latitude = number_format(doubleval($latitude), );
$gps = $latitude . ',' . $longitude;
$params = array(
'location' => $gps,
'pois' => ,
);
$info = test2::get_contents($url, $params);
print_r($info);
}
class test2{ private static function get_contents($url, $param)
{
$baidu_key = test::test_key ;
$param['ak'] = $baidu_key;
$param['output'] = 'json';
$url = $url . '?' . http_build_query($param, '', '&');
//echo $url;
$retry = ;
$result = array(); while ($retry > ) {
$result = json_decode(self::curl_get($url), true); if (!empty($result) && isset($result['status']) && $result['status'] == ) {
return $result;
}
if (!empty($result) && isset($result['status'])) {
$status = $result['status'];
} else {
$status = ' http_error:';
}
EdjLog::info("baidu_response retry status is" . $status.'params'.json_encode($param));
$retry--;
}
EdjLog::warning('request to baidu lbs api failed after retries'); return $result;
} private static function curl_get($url, $milliseconds = )
{
$start_time = microtime(true); $ch = curl_init();
//这个参数很重要,设置这个才可以支持毫秒级的超时设置
curl_setopt($ch, CURLOPT_NOSIGNAL, );
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_USERAGENT, self::$useragent);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FAILONERROR, );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, self::$connecttimeout);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $milliseconds); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
$http_error_code = curl_errno($ch);
curl_close($ch); EdjLog::info("request to baidu lbs api, url is $url, cost time:" . (microtime(true) - $start_time));
EdjLog::info("baidu http_error:$http_error_code response is " . str_replace(PHP_EOL, '', $response));
return $response;
}

结果:

Array
(
[status] =>
[result] => Array
(
[location] => Array
(
[lng] => 113.598909
[lat] => 37.863036027778
) [formatted_address] => 山西省阳泉市城区德胜街130号
[business] =>
[addressComponent] => Array
(
[country] => 中国
[country_code] =>
[country_code_iso] => CHN
[country_code_iso2] => CN
[province] => 山西省
[city] => 阳泉市
[city_level] =>
[district] => 城区
[town] =>
[adcode] =>
[street] => 德胜街
[street_number] => 130号
[direction] => 北
[distance] =>
) [pois] => Array
(
) [roads] => Array
(
) [poiRegions] => Array
(
[] => Array
(
[direction_desc] => 内
[name] => 阳泉站
[tag] => 交通设施;火车站
[uid] => a2ef2cba1b2e6961ab0dd8da
[distance] =>
) ) [sematic_description] => 阳泉站内
[cityCode] =>
) )
05-11 18:12