【问题标题】:Google Maps API Key Error谷歌地图 API 密钥错误
【发布时间】:2013-12-11 17:35:47
【问题描述】:

我正在尝试使用 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 页面https://code.google.com/apis/console/ 上为服务器(由 IP 地址设置)和浏览器(由域名设置)配置我的 API 密钥

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

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

如果我尝试在相关计算机上的网络浏览器中访问该 url https://maps.google.com/maps/api/geocode/json?address=The%20White%20House,%20Washington&sensor=false&key={MY_KEY} 我遇到了同样的问题:

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

服务器类型:

  • 操作系统: Windows Server 2008 R2 x64
  • 服务器: UwAmp 2.2.1
  • PHP: 5.4.15
  • MySQL: 5.6.11

提前感谢您的帮助!

【问题讨论】:

标签: php json google-maps curl geocoding


【解决方案1】:

使用 google map API 不需要 API 密钥。在这种情况下,谷歌回复了一条令人困惑的错误消息。只需从您的 URL 中删除“&key={MY_KEY}”,就可以了。

【讨论】:

【解决方案2】:

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

【讨论】:

  • 这个“解决方案”与问题无关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 2013-01-06
  • 1970-01-01
相关资源
最近更新 更多