Skip to content

Instantly share code, notes, and snippets.

@MattKetmo
Created October 9, 2013 08:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattKetmo/6897944 to your computer and use it in GitHub Desktop.
Save MattKetmo/6897944 to your computer and use it in GitHub Desktop.
Google Maps Sign URL
#!/usr/bin/env php
<?php
/**
* @see https://github.com/geocoder-php/Geocoder/blob/21e562a5ad595c6fee7a33ae90e0b42dc8866c23/src/Geocoder/Provider/GoogleMapsBusinessProvider.php#L82
*/
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>
USAGE;
if (count($argv) < 3) {
echo $usage.PHP_EOL;
exit(1);
}
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