【问题标题】:Google Map Geocode API when API key given returns REQUEST_DENIED给定 API 密钥时的 Google Map Geocode API 返回 REQUEST_DENIED
【发布时间】:2016-03-23 23:42:04
【问题描述】:

当我删除 API 密钥部分时,它工作正常

$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $string."&sensor=false";

添加 api 密钥时,会显示 REQUEST_DENIED。

$apiKey = 'AIzaSyCy2C82dDZlHkwGZ_fCfgh5gBdo50Q8cE0';
$string = str_replace(" ", "+", urlencode($string));
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $string."&sensor=false&key=".$apiKey;
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);

这是我第一次使用地理编码 API,我需要基于 API 密钥工作,因为一旦我们找不到足够的每天 2500 个查询,我们将购买按需付费计划。 (企业用谷歌地图)

我创建的 API 密钥是控制台面板上的新服务器密钥。

我做错了什么不接受我的 api 密钥?但是,当我添加 API 密钥并通过浏览器尝试时,它可以正常工作,如下所示,我可以在 google 控制台上看到使用情况报告和使用配额。

https://maps.googleapis.com/maps/api/geocode/json?address=2140+Amphitheatre+Parkway,+Mountain+View,+IN&key=AIzaSyCUDSJ2GBE1DHupbAZT4u8gZqclkIhmb0M

【问题讨论】:

标签: php api google-maps google-maps-api-3 google-geocoding-api


【解决方案1】:

api 请求必须先通过https 发送,然后您会发现密钥已过期。

$string = 'Dundee, Scotland';
$apiKey = 'AIzaSyCy2C82dDZlHkwGZ_fCfgh5gBdo50Q8cE0';
$string = str_replace( " ", "+", urlencode( $string ) );
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . $string."&sensor=false&key=".$apiKey;

$cacert='c:/wwwroot/cacert.pem';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CAINFO, realpath( $cacert ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,2 );

$response = json_decode( curl_exec( $ch ), true );
$info = curl_getinfo( $ch );
curl_close( $ch );


echo '<pre>', print_r($info,1), PHP_EOL, print_r( $response, 1 ), '</pre>';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 2017-04-19
    • 2013-07-04
    • 1970-01-01
    • 2013-12-01
    • 2013-04-19
    • 1970-01-01
    相关资源
    最近更新 更多