Can someone please take a look at this callback script for webhooks and let me know if there’s something off? I didn’t write it, it’s been working fine since last year up until a few days ago and then apparently it’s been failing to respond to some of the incoming data, and then today we had our hype train progress and cheers subs revoked. I have tried re-subbing, I had a saved query using Postman which has all the correct data and had previously worked - the app token is up to date, I got the pending verification message, but nothing has happened after that and I’m clueless as to what might have gone wrong. I’ve taken out our secret word and the database connection details - otherwise it’s all there.
<?php
date_default_timezone_set('UTC');
$date = new DateTime(NULL, new DateTimeZone( "UTC" ));
$timenow = $date->getTimeStamp();
$GD = getdate($timenow);
$logid = "-".$GD['month']."-".$GD['year'];
$logfile = "/home/fatstrs/shaminc/web/logs/webhook".$logid.".log";
$has = "";
$lh = fopen($logfile, 'a');
$sql = "";
$body = file_get_contents("php://input");
$data = json_decode($body, true);
fwrite($lh, $date->format('Y-m-d H:i:s T') . "\n");
fwrite($lh, "Headers: " . json_encode(getallheaders()) . "\n");
fwrite($lh, "Body: " . json_encode($data) . "\n");
fwrite($lh, "Get: " . json_encode($_GET) . "\n");
fwrite($lh, "Post: " . json_encode($_POST) . "\n\n\n");
if(isset(getallheaders()['Twitch-Eventsub-Message-Signature'])) {
$hdrs = getallheaders();
$hmac_message = $hdrs['Twitch-Eventsub-Message-Id'] . $hdrs['Twitch-Eventsub-Message-Timestamp'] . $body;
fwrite($lh, "HashedMessage:\n" .$hmac_message . "\n\n");
$signature = hash_hmac("sha256", $hmac_message, "XXXXXXXXXX");
$hash = 'sha256=' . $signature;
$validation = $hdrs['Twitch-Eventsub-Message-Signature'];
fwrite($lh, "Hashes:\n Calculated: " . $hash . "\n Supplied: " .$validation . "\n");
if($validation == $hash) {
fwrite($lh, "Validated HASH\n\n");
if(isset($data["challenge"])) {
fwrite($lh, "\"challenge\" included, responding:\n" . $data["challenge"] . "\n\n");
echo $data["challenge"];
}
} else {
fwrite($lh, "Invalid HASH\n\n");
}
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$headers = $conn->real_escape_string(json_encode($hdrs));
$body = $conn->real_escape_string(json_encode($data));
$twitch_id = $hdrs['Twitch-Eventsub-Message-Id'];
$twitch_ts = $hdrs['Twitch-Eventsub-Message-Timestamp'];
$twitch_ts = preg_replace("/\.[0-9]*/", "", $twitch_ts);
$twitch_ts = New DateTime($twitch_ts);
$twitch_ts = $twitch_ts->getTimestamp();
$received_ts = $date->getTimestamp();
$topic = $hdrs["Twitch-Eventsub-Subscription-Type"];
$sql = "INSERT INTO `TWITCH_Webhooks` (TwitchID, TwitchTS, ReceivedTS, Topic, Headers, Body) VALUES " .
"('" .$twitch_id . "', '" . $twitch_ts . "', '" . $received_ts . "', '" . $topic . "', '" . $headers . "', '" . $body . "')";
fwrite($lh, "SQL: " . $sql . "\n\n\n");
$result = $conn->query($sql);
}
fclose($lh);
exit();