如何从php curl json中获取反向地理编码


How to get reverse geocode from php curl json

我只想从json中回显fromatted_address,但不知道如何回显
以下代码:

    <?PHP
$ch = curl_init();
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=22.569794,88.357934';
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_HEADER, False);
curl_setopt($ch, CURLOPT_NOBODY, False);   //only header 
print "<pre>";
$bko = print_r(curl_exec($ch));
print "</pre>";
?>

您只需要将返回的json字符串传递到json_decode中,将其转换为数组,然后像往常一样访问数组的密钥。

$array = json_decode($json_result, true);
foreach($array['results'] as $k => $result) {
 echo $result['formatted_address'] . PHP_EOL;   
}

返回

印度西孟加拉邦加尔各答Bowbazar Chittaranjan Ave 50号,邮编:700012

CR Ave Rd,Bowbazar,加尔各答,西孟加拉邦700012,印度

Bowbazar、加尔各答、西孟加拉邦、印度

加尔各答,西孟加拉邦700001,印度

加尔各答,西孟加拉邦700012,印度

加尔各答、西孟加拉邦、印度

印度西孟加拉邦

印度

示例