admob server side verification php

Solutions on MaxInterview for admob server side verification php by the best coders in the world

showing results for - "admob server side verification php"
Rick
02 Nov 2018
1// Google admob public key
2$verifier_keys = '{"keys":[{"keyId":3335741209,"pem":"-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE+nzvoGqvDeB9+SzE6igTl7TyK4JB\nbglwir9oTcQta8NuG26ZpZFxt+F2NDk7asTE6/2Yc8i1ATcGIqtuS5hv0Q==\n-----END PUBLIC KEY-----","base64":"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE+nzvoGqvDeB9+SzE6igTl7TyK4JBbglwir9oTcQta8NuG26ZpZFxt+F2NDk7asTE6/2Yc8i1ATcGIqtuS5hv0Q=="}]}';
3
4// Google callback parameter string, get parameter
5$query_string = '';
6
7// json format public key string, array conversion, query according to callback parameter_ Key in string_ ID takes the corresponding public key to verify the signature
8$verifier_keys_arr = json_decode($verifier_keys, true);
9if(empty($verifier_keys_arr) || !is_array($verifier_keys_arr)){
10    throw new Exception("wrong google public keys!");
11}
12// Two formats of public key, pem and base64 
13$publicKey_pem = $verifier_keys_arr['keys'][0]['pem'];
14$publicKey_base64 = $verifier_keys_arr['keys'][0]['base64'];
15// Format of base64
16$publicKeyString = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($publicKey_base64, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
17// pem to public key resource object
18$publicKey = openssl_pkey_get_public($publicKeyString);
19// Note: publickey_ PEM, publickeystring and publickey can be signed normally
20
21// Parsing callback parameters
22parse_str($query_string, $query_arr);
23// Signature result string
24$signature = trim($query_arr['signature']);
25// It is important to replace and complement the signature result string here
26$signature = str_replace(['-', '_'], ['+', '/'], $signature);
27$signature .= '===';
28
29// Data element string to sign
30$message = substr($query_string, 0, strpos($query_string, 'signature')-1);
31
32$return = [
33    'code' => 0,
34    'message' => 'error'
35];
36
37//Verify the signature. Use $publicKey and $publicKey here_ PEM and $publickeystring are all OK
38$success = openssl_verify($message, base64_decode($signature), $publicKey, OPENSSL_ALGO_SHA256);
39if ($success === -1) {
40    $return['message'] = '111111'.openssl_error_string();
41} elseif ($success === 1) {
42    $return['code'] = 1;
43    $return['message'] = 'success';
44} else {
45    $return['message'] = '222222'.openssl_error_string();
46}
47
48var_dump($return);
49