-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-sms-verification-signed-request.php
77 lines (61 loc) · 2.24 KB
/
test-sms-verification-signed-request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
const URL = "https://verificationapi-v1.sinch.com/verification/v1/verifications";
/*
The key from one of your Verification Apps, found here https://dashboard.sinch.com/verification/apps
*/
$applicationKey = "<REPLACE_WITH_VERIF_APP_KEY>";
/*
The secret from the Verification App that uses the key above, found here https://dashboard.sinch.com/verification/apps
*/
$applicationSecret = "<REPLACE_WITH_VERIF_APP_SECRET>";
/*
The number that will receive the SMS PIN. Test accounts are limited to verified numbers.
The number must be in E.164 Format, e.g. Netherlands 0639111222 -> +31639111222
*/
$toNumber = "<REPLACE_WITH_TO_NUMBER>";
$smsVerficationPayload = [
"identity" => [
"type" => "number",
"endpoint" => $toNumber
],
"method" => "sms"
];
$encodedPayload = mb_convert_encoding(json_encode($smsVerficationPayload, JSON_UNESCAPED_UNICODE), 'UTF-8');
$md5EncodedPayload = md5($encodedPayload, true);
$encodedMd5ToBase64Payload = base64_encode($md5EncodedPayload);
$httpVerb = 'POST';
$requestContentType = 'application/json; charset=UTF-8';
date_default_timezone_set('UTC');
$timeNow = date(DateTime::ATOM);
$requestTimeStamp = "x-timestamp:" . $timeNow;
$requestUriPath = "/verification/v1/verifications";
$stringToSign = $httpVerb . "\n"
. $encodedMd5ToBase64Payload . "\n"
. $requestContentType . "\n"
. $requestTimeStamp . "\n"
. $requestUriPath;
$b64DecodedApplicationSecret = base64_decode($applicationSecret, true);
$calculatedSignature = base64_encode(hash_hmac("sha256", $stringToSign, $b64DecodedApplicationSecret, true));
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_HTTPHEADER => [
"content-type: {$requestContentType}",
"x-timestamp: {$timeNow}",
"authorization: application {$applicationKey}:{$calculatedSignature}"
],
CURLOPT_POSTFIELDS => json_encode($smsVerficationPayload),
CURLOPT_URL => URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $httpVerb,
]);
$response = curl_exec($curl);
$error = curl_error($curl);
$statusCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
curl_close($curl);
if ($error) {
echo "cURL Error #:" . $error . "\n";
} else {
echo $response . "\n";
echo $statusCode . "\n";
}