谷歌地图高程API不返回结果(JSON/PHP)


Google Maps Elevation API Not Returning Results (JSON/PHP)

我试图使用谷歌地图海拔API,但它没有返回任何结果。我使用的代码如下:

<?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); ?>

注释部分正在使用对我有用的地理编码API,但一旦我将$results变量更改为Elevation API,它根本不返回任何结果。当我在浏览器中输入URL时,我得到了结果。我已经在控制台中启用了API,并且有一个工作的API密钥。

有人知道为什么这不起作用吗?

确保在你的API控制台上Elevation API是启用的并且你的浏览器应用程序密钥Any referrer allowed或者如果你使用服务器应用程序密钥 Any IP allowed

否则尝试使用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);