PHP(Laravel&ThinkPHP)+百度实现地址转经纬度
作者:互联网
1. 使用PHP客户端GuzzleHttp
下载GuzzleHttp
composer require guzzlehttp/guzzle
2. 代码
use GuzzleHttp\Client;
// $addr为地址
// config('addr')=>http://api.map.baidu.com/geocoding/v3/?address=%s&output=json&ak=CxOKZ1tLK5U97ytZFFG6H2kEKTSPpMSm&callback=showLocation
class Latlon{
public static function getinfo($addr)
{
$url = sprintf(config('addr'),$addr);
$client = new Client(['timeout' => 5,'verify' => false]);
$response = $client->get($url);
$json = (string)$response->getBody();
$v['lng'] = substr(explode('lng":',$json)[1],0,10);
$v['lat'] = substr(explode('lat":',$json)[1],0,10);
dd($v);
}
}
标签:Laravel,addr,url,GuzzleHttp,substr,json,ThinkPHP,lng,PHP 来源: https://blog.csdn.net/qq_50934800/article/details/119250214