2

Im trying the echo the elevation of an address in PHP. I am using the Google Elevation API - LINK

I already have the latitude and longitude. The following link:

https://maps.googleapis.com/maps/api/elevation/json?locations=40.7143528,-74.0059731&key=myapikey

will produce these results in json:

{ "results" : [ { "elevation" : 9.774918556213379, "location" : { "lat" : 40.7143528, "lng" : -74.00597310000001 }, "resolution" : 19.08790397644043 } ], "status" : "OK" }

How can I echo the elevation? something like <?php echo 'elevation'; ?>

Thank you

Schalk Joubert
  • 398
  • 3
  • 19
  • Hey, you shared your private API_KEY. I suggest you remove it from the question and regenerate a new for you from API Console – ARLabs Feb 08 '18 at 11:01

2 Answers2

5

You can do this by changing the response to be xml format to make things easier:

https://maps.googleapis.com/maps/api/elevation/xml?locations=40.7143528,-74.0059731&key=yourKey From there if your php code supports it you can do this:

<?php
$response = file_get_contents('https://maps.googleapis.com/maps/api/elevation/xml?locations=40.7143528,-74.0059731&key=myapikey');
$results = new SimpleXMLElement($response);
echo $results->elevation;
?>
Schalk Joubert
  • 398
  • 3
  • 19
Franco Vizcarra
  • 428
  • 2
  • 11
  • Thank you. I get 2 errors. The one I may hace fixed with `function file_get_contents_curl( $url ) { $ch = curl_init(); curl_setopt( $ch, CURLOPT_AUTOREFERER, TRUE ); curl_setopt( $ch, CURLOPT_HEADER, 0 ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE ); $data = curl_exec( $ch ); curl_close( $ch ); return $data; }` and changing to `file_get_contents_curl` [link]https://gist.github.com/schalkjoubert/0d9be4969990e37dda6594347e8ce983 – Schalk Joubert Feb 07 '18 at 11:03
  • The problem appears to be that you cannot connect due to a certificate verification error. [This is not needed and you can disable it](https://stackoverflow.com/questions/37858367/curl-not-working-while-fetching-google-api-data). – Franco Vizcarra Feb 07 '18 at 11:48
  • Thank you, tried that, and got a bit further. I do admit, I do admit, I'm treading in an area way above my level. `$url = "https://maps.googleapis.com/maps/api/elevation/json?locations=40.7143528,-74.0059731&key=AIzaSyCM1fCTYKPhk6tDCqsVNhcXto6aqjUc0yY"; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL,$url); $result=curl_exec($ch); curl_close($ch); print json_encode($result, JSON_PRETTY_PRINT);` This prints everything, including elevation. – Schalk Joubert Feb 07 '18 at 12:47
  • This is what i see on my screen: `"{\n \"results\" : [\n {\n \"elevation\" : 9.774918556213379,\n \"location\" : {\n \"lat\" : 40.7143528,\n \"lng\" : -74.00597310000001\n },\n \"resolution\" : 19.08790397644043\n }\n ],\n \"status\" : \"OK\"\n}\n"` – Schalk Joubert Feb 07 '18 at 12:50
  • That's because you have `print json_encode($result, JSON_PRETTY_PRINT);` at the end of your code. Just remove that line and it'll stop print out to screen. – Franco Vizcarra Feb 07 '18 at 12:51
  • Keep your chin up, if you search Stack Overflow for each error that you get. I'm sure you'll get it in the end. I can see that my answers have been useful, I'd be very grateful for recognition through an upvote. Thanks. – Franco Vizcarra Feb 07 '18 at 12:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164694/discussion-between-franco-vizcarra-and-user1664798). – Franco Vizcarra Feb 07 '18 at 12:57
1

The thread is old but I found an alternative that can be used to do an elevation query without google and without a key. The link: https://api.opentopodata.org/v1/eudem25m?locations=51.875127,10.65432 gives the following result:

{
  "results": [
    {
      "dataset": "eudem25m", 
      "elevation": 352.5389709472656, 
      "location": {
        "lat": 51.875127, 
        "lng": 10.65432
      }
    }
  ], 
  "status": "OK"
}

which can be evaluated with php:

$json = json_decode($result, true);
$result = $json['results'][0]['elevation'];

Note: when querying in a loop, a sleep must be included, e.g. sleep(2);

guenter47
  • 457
  • 5
  • 13