使用 PHP (curl) 从 JSON 中提取数据(Google 地图 API)
我对 JSON 相当陌生,我正在尝试使用curl 从 Google Maps API 获取地理编码城市的纬度和经度。我正在使用的函数是:
function geocode($city){
$cityclean = str_replace (" ", "+", $city);
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityclean . "&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$geoloc = json_decode(curl_exec($ch), true);
$step1 = $geoloc['results'];
$step2 = $step1['geometry'];
$coords = $step2['location'];
print $coords['lat'];
print $coords['lng'];
}
所有这些的目标是从结果中提取 lat 和 lng 值 ->几何->以下 JSON 的位置数组:
{
"status": "OK",
"results": [ {
"types": [ "locality", "political" ],
"formatted_address": "Westminster, London, UK",
"address_components": [ {
"long_name": "London",
"short_name": "London",
"types": [ "locality", "political" ]
}, {
"long_name": "Westminster",
"short_name": "Westminster",
"types": [ "administrative_area_level_3", "political" ]
}, {
"long_name": "Greater London",
"short_name": "Greater London",
"types": [ "administrative_area_level_2", "political" ]
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 51.5001524,
"lng": -0.1262362
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 51.3493528,
"lng": -0.3783580
},
"northeast": {
"lat": 51.7040647,
"lng": 0.1502295
}
},
"bounds": {
"southwest": {
"lat": 51.3493528,
"lng": -0.3783580
},
"northeast": {
"lat": 51.7040647,
"lng": 0.1502295
}
}
}
} ]
}
但是,它不会打印任何内容。我知道该函数成功从 Google 检索 JSON 并将其带到我的服务器,因为我对“状态”值执行了打印语句,并且它返回了“确定”。问题似乎出在我尝试深入研究 JSON 时。
抱歉,如果这是一个简单的问题,但就像我说的,我对此很陌生,现在它让我发疯。
多谢! :)
I'm fairly new to JSON, and I'm trying to get the latitude and longitude of a geocoded city from the Google Maps API using curl. The function I'm using is:
function geocode($city){
$cityclean = str_replace (" ", "+", $city);
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityclean . "&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$geoloc = json_decode(curl_exec($ch), true);
$step1 = $geoloc['results'];
$step2 = $step1['geometry'];
$coords = $step2['location'];
print $coords['lat'];
print $coords['lng'];
}
The goal of all of that is to pull the lat and lng values from the Results -> Geometry -> Location array of the following JSON:
{
"status": "OK",
"results": [ {
"types": [ "locality", "political" ],
"formatted_address": "Westminster, London, UK",
"address_components": [ {
"long_name": "London",
"short_name": "London",
"types": [ "locality", "political" ]
}, {
"long_name": "Westminster",
"short_name": "Westminster",
"types": [ "administrative_area_level_3", "political" ]
}, {
"long_name": "Greater London",
"short_name": "Greater London",
"types": [ "administrative_area_level_2", "political" ]
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 51.5001524,
"lng": -0.1262362
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 51.3493528,
"lng": -0.3783580
},
"northeast": {
"lat": 51.7040647,
"lng": 0.1502295
}
},
"bounds": {
"southwest": {
"lat": 51.3493528,
"lng": -0.3783580
},
"northeast": {
"lat": 51.7040647,
"lng": 0.1502295
}
}
}
} ]
}
However, it doesn't print anything. I know that the function successfully retrieves the JSON from Google and brings it to my server, as I did a print statement for the 'status' value and it returned 'OK'. The problem seems to be when I try and delve deeper into the JSON.
Sorry if it's a straightforward problem, but like I said, I'm new to this and it's driving me crazy now.
Thanks a lot! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,
results
似乎包含一个结果数组 (此处仅包含一项);geometry
是一个结果中的一项。在这里,您可以看到结果的内容由
[]
分隔——这表明它是一个数组。因此,您必须首先访问第一个结果:
$geoloc['results'][0]
其中您将拥有几何图形:
$geoloc['results'][0]['geometry']
这将允许您获取纬度和经度:
我认为,根据您搜索的地址,
results
数组中有时会包含多个项目。Note that it seems that
results
contains an array (with only one item in it, here) of results ; andgeometry
is one item inside one result.Here, you can see that results' content is delimited by
[]
-- which indicates it's an array.So, you have to first access the first result :
$geoloc['results'][0]
Inside of which you'll have the geometry :
$geoloc['results'][0]['geometry']
Which will allow you to get the latitude and longitude :
I suppose that, depending on the address you've searched on, you will sometimes have more than one item in the
results
array.你只需要这个:
You just need this :