Skip to content

Instantly share code, notes, and snippets.

@dexit
Forked from gingerbeardman/webhook.php
Created February 10, 2025 23:11
Show Gist options
  • Save dexit/1c878c694d61dde7ee85a8de7490bfc7 to your computer and use it in GitHub Desktop.
Save dexit/1c878c694d61dde7ee85a8de7490bfc7 to your computer and use it in GitHub Desktop.
Webhook receiver PHP script
<?php
// GitHub webhook secret (set this in your GitHub webhook settings)
$secret = "Y0UR-secret-text-here!";
// Get the payload
$payload = file_get_contents('php://input');
// Verify the signature
$signature = $_SERVER['HTTP_X_HUB_SIGNATURE'] ?? null;
if (!$signature) {
exit("No signature provided");
}
$hash = "sha1=" . hash_hmac('sha1', $payload, $secret);
if (!hash_equals($hash, $signature)) {
exit("Invalid signature");
}
// Parse the payload
$data = json_decode($payload, true);
// Check if it's a push event
if ($_SERVER['HTTP_X_GITHUB_EVENT'] == 'push') {
$triggerFile = '/var/www/example.com/triggers/git_pull_trigger';
// Try to create trigger file
$result = file_put_contents($triggerFile, date('Y-m-d H:i:s'));
if ($result === false) {
exit("Failed to create trigger file");
}
echo "Webhook received and trigger file created";
} else {
echo "Received non-push event";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment