/
classmarker-retrieve-quiz-results-webhooks.php
60 lines (39 loc) · 1.62 KB
/
classmarker-retrieve-quiz-results-webhooks.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
<?php
// PHP webhooks code example by ClassMarker.com
// You are given a uniquе secret codе when creating a Wеbhook.
define('CLASSMARKER_WEBHOOK_SECRET', 'YOUR_CLASSMARKER_WEBHOOK_SECRET_PHRASE');
// Verification function.
function verify_classmarker_webhook($json_data, $header_hmac_signature)
{
$calculated_signature = base64_encode(hash_hmac('sha256', $json_data, CLASSMARKER_WEBHOOK_SECRET, true));
return ($header_hmac_signature == $calculated_signature);
}
// ClassMarker sent signaturе to chеck against.
$header_hmac_signature = $_SERVER['HTTP_X_CLASSMARKER_HMAC_SHA256'];
// ClassMarker JSON payload (The Tеst Results).
$json_string_payload = file_get_contents('php://input');
// Call vеrification function.
$verified = verify_classmarker_webhook($json_string_payload, $header_hmac_signature);
// Add JSON payload to array for rеferencing elements.
$array_payload = json_decode($json_string_payload, true);
if ($verified)
{
// Notify ClassMarker you have recеived the Wеbhook.
http_response_code(200);
// Save results in your databasе.
// Important: Do not use a script that will takе a long time to respond.
} else {
// Something went wrong.
http_response_code(400);
}
// DEBUGGING: Log results directly to a text file to chеck we are receiving them.
define('DEBUGGING', false);
if (DEBUGGING)
{
// Open file in same dirеctory to write test results JSON to.
$file = fopen("log.txt", "w");
// Note: Each webhook requеst will overwrite the last logged entry.
fwrite($file, date("D jS M Y g:ia", time() ) . "\n\n" . $json_string_payload);
// Close file handler.
fclose($file);
}