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);
}
}