Created
October 9, 2013 08:16
-
-
Save MattKetmo/6897944 to your computer and use it in GitHub Desktop.
Google Maps Sign URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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