Skip to content

Instantly share code, notes, and snippets.

@viralgh
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viralgh/3cdd6ab73f9d58e99a95 to your computer and use it in GitHub Desktop.
Save viralgh/3cdd6ab73f9d58e99a95 to your computer and use it in GitHub Desktop.
Git-hub Web hook - PHP
<?php
// Content Type: application/json
$time = time();
$secret = '<your-secret-here>';
$headers = getallheaders();
$hubSignature = @$headers['X-Hub-Signature'];
if ($hubSignature != '')
{
list($algo, $hash) = @explode('=', $hubSignature, 2);
$payload = file_get_contents('php://input');
if (isset($algo) and isset($hash))
{
if ($algo != '' and $hash != '')
{
$payloadHash = hash_hmac($algo, $payload, $secret);
}
else
{
die('Bad request');
}
if ($hash !== $payloadHash)
{
die('Bad request');
}
}
else
{
die('Bad request');
}
}
else
{
die('Bad request');
}
echo "Webhook received.\nTimestamp: {$time}\n--- start ---";
$data = json_decode($payload);
// process data
echo "\n\n--- done ---";
?>
/* on Success Response
Webhook received.
Timestamp: 1427803925
--- start ---
--- done ---
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment