Google Maps API Key Error

我是研究僧i 提交于 2019-12-24 11:44:17

问题


I'm trying to connect my PHP script to the google maps API using CURL it connects fine but when i pass the API key it says that it is not valid, below is some of the code I'm using to get stats...

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

I have tried configuring my API keys for both server (set by IP address) and browser (set by Domain Name) on the Google API page at https://code.google.com/apis/console/

The problem that I'm having is every time i send a request i get a return JSON message saying:

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

If i try to goto the url in a web browser on the computer in question https://maps.google.com/maps/api/geocode/json?address=The%20White%20House,%20Washington&sensor=false&key={MY_KEY} i get the same problem:

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

Server type:

  • O/S: Windows Server 2008 R2 x64
  • Server: UwAmp 2.2.1
  • PHP: 5.4.15
  • MySQL: 5.6.11

Thanks in advance for any help!


回答1:


The API key is not required for your use of google map API. It is a confusing error message that google replies with in this case. Just remove from your URL the '&key={MY_KEY}', and you should be fine.




回答2:


You could create table location in your mysql.every time you send a request .insert it to databse. after that you can count request by using count record in your database.i used this solution



来源:https://stackoverflow.com/questions/20526091/google-maps-api-key-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!