谷歌地图API密钥错误


Google Maps API Key Error

我试图使用CURL将我的PHP脚本连接到谷歌地图API,它连接良好,但当我传递API键时,它会说它无效,下面是我用来获取统计数据的一些代码。。。

<?php
    $url = 'https://maps.google.com/maps/api/geocode/json?address={ADDRESS}&sensor=false&key={MY_KEY}';
    $cURL = curl_init();
    curl_setopt($cURL, CURLOPT_URL, $url);
    curl_setopt($cURL, CURLOPT_HTTPGET, true);
    curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
                                                'Content-Type: application/json',
                                                'Accept: application/json'
                                           ));
    curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($cURL);
    curl_close($cURL);
?>

我已尝试在Google API页面上为服务器(由IP地址设置)和浏览器(由域名设置)配置API密钥,网址为https://code.google.com/apis/console/

我遇到的问题是,每次我发送请求时,都会收到一条返回的JSON消息,上面写着:

stdClass Object (
    [error_message] => This site or IP is not authorized to use this API key.
    [results] => Array (
    )
    [status] => REQUEST_DENIED
)

如果我试图在有问题的计算机上的网络浏览器中获取urlhttps://maps.google.com/maps/api/geocode/json?address=The%20White%20House,%20华盛顿;传感器=错误&key={MY_key}我遇到了同样的问题:

{
   "error_message" : "This site or IP is not authorized to use this API key.",
   "results" : [],
   "status" : "REQUEST_DENIED"
}

服务器类型:

  • O/S:Windows Server 2008 R2 x64
  • 服务器:UwAmp 2.2.1
  • PHP:5.4.15
  • MySQL:5.6.1

提前感谢您的帮助!

API密钥对于您使用谷歌地图API是不需要的。在这种情况下,谷歌回复的是一条令人困惑的错误消息。只需从您的URL中删除'&key={MY_key}',您应该没事。

每次发送请求时,都可以在mysql中创建表位置。将其插入数据库。之后,您可以使用数据库中的计数记录来计数请求。我使用了这个解决方案