从 GOOGLE MAPS GEOLOCATION API 在 PHP 获得响应,输出在 JSON
Getting response from GOOGLE MAPS GEOLOCATION API in PHP, the output is in JSON
我一直在到处寻找答案,没有找到。我正在使用 google 地图地理定位 api 来显示经纬度的地址。我只需要显示选择性数据,例如:数组中的 "formatted_address"。这是我试图在 PHP:
中显示响应的代码
$file_contents = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=31.334097,74.235875&sensor=false", true);
$data = json_decode($file_contents, true);
foreach($data as $output => $var) {
echo $var;
}
这是回复:[https://maps.googleapis.com/maps/api/geocode/json?latlng=31.334097,74.235875]
这是我得到的输出:
Notice: Array to string conversion in C:\xampp\htdocs\bikewala\myaccount\index.php on line 176
ArrayOK
当我尝试使用此代码访问数组的任何组件时:
$file_contents = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=31.334097,74.235875&sensor=false", true);
$data = json_decode($file_contents, true);
foreach($data as $output => $var) {
echo $var['formatted_address'];
}
我收到这个错误:
Notice: Undefined index: formatted_address in C:\xampp\htdocs\bikewala\myaccount\index.php on line 176
Warning: Illegal string offset 'formatted_address' in C:\xampp\htdocs\bikewala\myaccount\index.php on line 176
O
我做错了什么?
提前致谢!
而不是回显(在您的第一个代码部分)使用 print_r($var)
$file_contents =
file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?
latlng=31.334097,74.235875&sensor=false", true);
$data = json_decode($file_contents, true);
foreach($data as $output => $var) {
print_r($var);
}
这将使您能够看到您想要的值的路径
您可以使用此代码访问您想要的确切值
$file_contents = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?
latlng=31.334097,74.235875&sensor=false", true);
$data = json_decode($file_contents, true);
$formatted_address = $data['results']['0']['formatted_address'];
echo $formatted_address;
我一直在到处寻找答案,没有找到。我正在使用 google 地图地理定位 api 来显示经纬度的地址。我只需要显示选择性数据,例如:数组中的 "formatted_address"。这是我试图在 PHP:
中显示响应的代码$file_contents = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=31.334097,74.235875&sensor=false", true);
$data = json_decode($file_contents, true);
foreach($data as $output => $var) {
echo $var;
}
这是回复:[https://maps.googleapis.com/maps/api/geocode/json?latlng=31.334097,74.235875]
这是我得到的输出:
Notice: Array to string conversion in C:\xampp\htdocs\bikewala\myaccount\index.php on line 176
ArrayOK
当我尝试使用此代码访问数组的任何组件时:
$file_contents = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=31.334097,74.235875&sensor=false", true);
$data = json_decode($file_contents, true);
foreach($data as $output => $var) {
echo $var['formatted_address'];
}
我收到这个错误:
Notice: Undefined index: formatted_address in C:\xampp\htdocs\bikewala\myaccount\index.php on line 176
Warning: Illegal string offset 'formatted_address' in C:\xampp\htdocs\bikewala\myaccount\index.php on line 176
O
我做错了什么?
提前致谢!
而不是回显(在您的第一个代码部分)使用 print_r($var)
$file_contents =
file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?
latlng=31.334097,74.235875&sensor=false", true);
$data = json_decode($file_contents, true);
foreach($data as $output => $var) {
print_r($var);
}
这将使您能够看到您想要的值的路径
您可以使用此代码访问您想要的确切值
$file_contents = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?
latlng=31.334097,74.235875&sensor=false", true);
$data = json_decode($file_contents, true);
$formatted_address = $data['results']['0']['formatted_address'];
echo $formatted_address;