Elevation Google Maps API returns no results (JSON / PHP)

I'm trying to use the google maps add-on API but it doesn't return any results. The code I'm using is below:

<?php
$results = file_get_contents("https://maps.googleapis.com/maps/api/elevation/json?locations=-37.8,144.815&key=[myAPIkey]&sensor=false");
//$results = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=Paris,France&sensor=false&key=[myAPIkey]");
$json = json_decode($results, true);
echo ${results};
//print_r($json); ?>

      

The commented parts use the geocoding API, which works for me, but once I change the $ results variable in the Elevation API, it returns no results at all. When I put the url in my browser, I get the results. I enabled the API in the console and had a working API key.

Anyone have any ideas why this isn't working?

+3


source to share


1 answer


Make sure on your API console that the Elevation API is enabled and your browser has Any referrer allowed

or if you are using a server app key >Any IP allowed

Otherwise try using cURL



$curlUrlRequest = 'https://maps.googleapis.com/maps/api/elevation/json?locations=-37.8,144.815&key=[myApiKey]&sensor=false';

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $curlUrlRequest);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
var_dump($response);

      

+2


source







All Articles