Last active
February 10, 2025 23:11
-
-
Save gingerbeardman/63e4dc0bce459ad6609c2701963eb61f to your computer and use it in GitHub Desktop.
Webhook receiver PHP script
This file contains 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 | |
// 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