[英]get data from external url using php json?
I'm trying to extract data from an external url using json in PHP using the following code: 我正在尝试使用以下代码在PHP中使用json从外部URL提取数据:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=37.76893497,-122.42284884&sensor=false";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
echo $json_data["formatted_address"];
?>
however, I get nothing on my page. 但是,我的页面上什么也没有。 in fact, i get this error: 实际上,我得到此错误:
Notice: Undefined index: formatted_address on line 7
is there something I'm missing? 有什么我想念的吗?
any help would be greatly appreciated. 任何帮助将不胜感激。
'formatted_address'
是主数组'results'
的键,因此您应循环$json_data['results']
并搜索键'formatted_address'
。
尝试这种方式
echo $json_data['results'][0]['formatted_address'];
You are not providing proper INDEX
. 您没有提供正确的INDEX
。 Proper INDEX
is $json_data['results'][0]['formatted_address'];
正确的INDEX
是$json_data['results'][0]['formatted_address'];
for 1st result. 对于第一个结果。
Use foreach
loop to print all address. 使用foreach
循环打印所有地址。
Try 尝试
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=37.76893497,-122.42284884&sensor=false";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
foreach($json_data['results'] as $item)
{
echo $item['formatted_address']."<br />";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.