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

发布于 2024-10-29 07:40:28 字数 2233 浏览 1 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书间行客 2024-11-05 07:40:28

请注意,results 似乎包含一个结果数组 (此处仅包含一项)geometry 是一个结果中的一项。

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

因此,您必须首先访问第一个结果:$geoloc['results'][0]

其中您将拥有几何图形:$geoloc['results'][0]['geometry']

这将允许您获取纬度和经度:

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

我认为,根据您搜索的地址,results 数组中有时会包含多个项目。

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.

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

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

感性不性感 2024-11-05 07:40:28

你只需要这个:

$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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文