编程语言
首页 > 编程语言> > Google Maps Elevation API未返回结果(JSON / PHP)

Google Maps Elevation API未返回结果(JSON / PHP)

作者:互联网

我正在尝试使用Google Maps Elevation 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,并且您的浏览器应用程序密钥具有允许的任何引荐来源网址,或者如果您使用服务器应用程序密钥,则允许任何IP

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

标签:elevation,api,maps,json,php
来源: https://codeday.me/bug/20191120/2042508.html