根据城市查询经纬度 php,通过经纬度获取所属城市信息-php

$url = 'http://api.map.baidu.com/geocoder/v2/';//37.863036

$longitude = number_format(doubleval($longitude), 6);//113.598909

$latitude = number_format(doubleval($latitude), 6);

$gps= $latitude . ','. $longitude;

$params =array('location' =>$gps,'pois' => 0,

);

$info= test2::get_contents($url, $params);

print_r($info);

}classtest2{private staticfunction 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 = 2;

$result=array();while ($retry > 0) {

$result= json_decode(self::curl_get($url), true);if (!empty($result) && isset($result['status']) && $result['status'] == 0) {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 = 300)

{

$start_time= microtime(true);

$ch=curl_init();//这个参数很重要,设置这个才可以支持毫秒级的超时设置

curl_setopt($ch, CURLOPT_NOSIGNAL, 1);

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,0);

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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值