Programming Rambling

mrzard's ramblings in the wild

Generate a Valid Google Premier Signature in PHP

| Comments

Google Premier requires for you to generate a signature over the URL you’re going to ask for, then send that signature alongside the request. Here is how to do it, as there is no PHP example in the Google Premier URL Signature documentation

In this code snippet we assume:

  • $request_url has the url that will be using the Google Premier service (for example, Static Maps API). It also already has the client param, sensor param, etc.
  • $signature_key has the key provided to you by Google
  • You are running this snippet from an authorized domain
  • Props to ZZ Coder at StackOverflow
Generate a valid Google Premier signature
1
2
3
4
5
6

$url_parts = parse_url($request_url); $signable_part = $url_parts['path'].'?'.$url_parts['query'];
$decoded_key = base64_decode(strtr($signature_key, '-_', '+/');
$url_signature = hash_hmac('sha1', $signable_part, $decoded_key), true);
$base64signature = strtr(base64_encode($url_signature), '+/', '-_');
$signature_param = '&signature='.urlencode($base64signature);

‘Strange’ things in this snippet:

  • Why the strtr()? Because Google uses URL-Safe base64
  • Why the true param at the end of hash_hmac? Because we need it the signature to be returned in binary before base64encondig it.

Then you just have to append $signature_param to your original request (which we’ve assumed is in $request_url) to have a correctly signed Google Premier request.

Comments