Created
November 4, 2019 16:08
-
-
Save pokap/c28d62d9bd23497d799d1ec27e8a9b78 to your computer and use it in GitHub Desktop.
Toornament webhook test php
This file contains hidden or 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
<?php | |
const FILE_LOG = 'webhook.log'; | |
ensure_log_access(); | |
switch ($_SERVER['REQUEST_METHOD']) { | |
case 'HEAD': | |
add_log("Subscription works!"); | |
send_headers_and_exit(200, 'OK', ['X-Webhook-Secret' => $_SERVER['HTTP_X_WEBHOOK_SECRET']]); | |
break; | |
case 'POST': | |
$requestContent = file_get_contents('php://input'); | |
add_log($requestContent); | |
send_headers_and_exit(200, 'OK'); | |
break; | |
case 'GET': | |
if (isset($_GET['action']) && $_GET['action'] === 'purge') { | |
purge_logs(); | |
} | |
$logs = get_logs(); | |
print "<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Webhook</title> | |
</head> | |
<body> | |
<h1>Logs</h1> | |
<pre>{$logs}</pre> | |
</body> | |
</html>"; | |
break; | |
} | |
exit(0); | |
// --------------------------------------------------------------------------------------------------------------------- | |
function send_headers_and_exit($statusCode, $statusText, array $headers = []) { | |
foreach ($headers as $name => $value) { | |
header("{$name}: {$value}", false, $statusCode); | |
} | |
header("HTTP/1.1 {$statusCode} {$statusText}", true, $statusCode); | |
exit(0); | |
} | |
function ensure_log_access() { | |
if (!file_exists(FILE_LOG)) { | |
touch(FILE_LOG); | |
} | |
} | |
function add_log($message) { | |
$date = new \DateTime('now', new \DateTimeZone('UTC')); | |
$message = str_replace("\n", "\n\t", $message); | |
file_put_contents(FILE_LOG, "[{$date->format(DATE_RFC3339)}]\n\t{$message}\n", FILE_APPEND); | |
} | |
function get_logs() { | |
return file_get_contents(FILE_LOG); | |
} | |
function purge_logs() { | |
file_put_contents(FILE_LOG, ''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment