Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Google Maps Sign URL
#!/usr/bin/env php
* @see
function signQuery($query, $privateKey)
$url = parse_url($query);
$urlPartToSign = $url['path'].'?'.$url['query'];
// Decode the private key into its binary format
$decodedKey = base64_decode(str_replace(array('-', '_'), array('+', '/'), $privateKey));
// 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 = str_replace(array('+', '/'), array('-', '_'), base64_encode($signature));
return sprintf('%s&signature=%s', $query, $encodedSignature);
$argv = $_SERVER['argv'];
$basename = $argv[0];
$usage = <<<USAGE
Usage: $basename <url> <private-key>
if (count($argv) < 3) {
echo $usage.PHP_EOL;
echo signQuery($argv[1], $argv[2]).PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.