使用 PHP (curl) 从 JSON (Google Maps API) 中提取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5525561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using PHP (curl) to pull data from JSON (Google Maps API)
提问by Hyman W
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:
我对 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'];
}
The goal of all of that is to pull the lat and lng values from the Results -> Geometry -> Location array of the following JSON:
所有这些的目标是从以下 JSON 的 Results -> Geometry -> Location 数组中提取 lat 和 lng 值:
{
"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.
但是,它不打印任何内容。我知道该函数成功地从 Google 检索了 JSON 并将其传送到我的服务器,因为我为 'status' 值做了一个打印语句,它返回了 'OK'。问题似乎出在我尝试深入研究 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! :)
非常感谢!:)
采纳答案by Pascal MARTIN
Note that it seems that results
contains an array(with only one item in it, here)of results ; and geometry
is one item inside one result.
请注意,它似乎results
包含一个结果数组(此处只有一项);并且geometry
是一个结果中的一项。
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']
因此,您必须首先访问第一个结果:$geoloc['results'][0]
在其中您将拥有几何图形:$geoloc['results'][0]['geometry']
Which will allow you to get the latitude and longitude :
这将允许您获得纬度和经度:
var_dump($geoloc['results'][0]['geometry']['location']['lat']);
var_dump($geoloc['results'][0]['geometry']['location']['lng']);
I suppose that, depending on the address you've searched on, you will sometimes have more than one item in the results
array.
我想,根据您搜索的地址,您有时会在results
数组中拥有多个项目。
回答by Fernando Cardenas 'Pelonxz'
You just need this :
你只需要这个:
$fullurl = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";
$string .= file_get_contents($fullurl); // get json content
$json_a = json_decode($string, true); //json decoder
echo $json_a['results'][0]['geometry']['location']['lat']; // get lat for json
echo $json_a['results'][0]['geometry']['location']['lng']; // get ing for json