使用PHP(curl)从JSON中提取数据(Google Maps API)

使用PHP(curl)从JSON中提取数据(Google Maps API)

问题描述:

我对JSON还是很陌生,我正在尝试使用curl从Google Maps API获取地理编码城市的纬度和经度.我正在使用的功能是:

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'];

}

所有这些操作的目标是从以下JSON的结果->几何->位置数组中拉出lat和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
        }
      }
    }
  } ]
}

但是,它不打印任何内容.我知道该函数成功地从Google检索了JSON并将其带到我的服务器,因为我为'status'值执行了打印语句,并且返回了'OK'.问题似乎出在我尝试更深入地研究JSON时.

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.

非常感谢! :)

请注意,似乎 results包含一个数组 (此处只有一个项)结果;而geometry是一个结果中的一项.

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.

在这里,您可以看到结果的内容由[]分隔-表示它是一个数组.

Here, you can see that results' content is delimited by [] -- which indicates it's an array.


因此,您必须首先访问第一个结果:$geoloc['results'][0]
其中将具有以下几何形状:$geoloc['results'][0]['geometry']


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 :

var_dump($geoloc['results'][0]['geometry']['location']['lat']);
var_dump($geoloc['results'][0]['geometry']['location']['lng']);


我想根据您搜索的地址,有时results数组中会有多个项目.


I suppose that, depending on the address you've searched on, you will sometimes have more than one item in the results array.