谷歌地图api秘密密钥问题

时间:2018-05-02 14:29:52

标签: javascript google-maps

我想在我的google maps api 中添加api密钥和签名。

从谷歌控制台生成api密钥和密钥后,我们是否需要执行加密等操作,或者我们可以直接在我们的网址中使用该密钥?

 $url .= "&key=abcdefghijklmnopqrstuvwxyz&signature=abcdefghijkliimnopqrstuvwxyz";

如果我使用签名,那么它不会加载我的地​​图,有什么解决方案?

1 个答案:

答案 0 :(得分:1)

$url='http://maps.googleapis.com/maps/api/staticmap?&size=$dimensions&maptype=roadmap&sensor=false&key=abcdefghijklmnopqrstuvwxyz'; 
$url1=$url;
$url = parse_url($url1);

$urlPartToSign = $url['path'] . "?" . $url['query'];

// Decode the private key into its binary format
$decodedKey = decodeBase64UrlSafe('abcdefghijklmnopqrstuvwxy');

// Create a signature using the private key and the URL-encoded
// string using HMAC SHA1. This signature will be binary.
$signature = hash_hmac("sha1",$urlPartToSign, $decodedKey,  true);

$encodedSignature = encodeBase64UrlSafe($signature);

$url=$url1."&signature=".$encodedSignature;

return saveImageUrlToDisk($imagePath, $url);

 }

function encodeBase64UrlSafe($value)
{
return str_replace(array('+', '/'), array('-', '_'),
    base64_encode($value));
}

// Decode a string from URL-safe base64
function decodeBase64UrlSafe($value)
{
return base64_decode(str_replace(array('-', '_'), array('+', '/'),
    $value));
}