Skip to content

Instantly share code, notes, and snippets.

@janzell
Created March 2, 2020 09:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janzell/ca5752c3368b630e830efef5f2e77b1f to your computer and use it in GitHub Desktop.
Save janzell/ca5752c3368b630e830efef5f2e77b1f to your computer and use it in GitHub Desktop.
Facebook Webhook - Verification
<?php
class Hooks {
/**
* Facebook App Secret Key
*/
protected $secretKey;
/**
* Webhook token which needed for Verification
*/
protected $token;
public function __construct(string $secretKey, string$token)
{
$this->token = $token;
$this->secretKey = $secretKey;
}
public function verify() {
if ($_GET('hub_mode') === 'subscribe'
&& $_GET('hub_verify_token') === $this->token) {
echo $_GET('hub_challenge');
} else {
echo 'invalid token';
}
}
/**
* Is valid x-hub signature
*/
protected function isValidXHubSignature($data, $header_signature)
{
$expected_signature = hash_hmac('sha1', $data, $this->secretKey);
$signature = '';
if (strlen($header_signature) == 45 && substr($header_signature, 0, 5) == 'sha1=') {
$signature = substr($header_signature, 5);
}
return hash_equals($signature, $expected_signature);
}
public function process()
{
$data = file_get_contents('php://input');
$header_signature = $_SERVER('HTTP_X-Hub-Signature');
if ($this->isValidXHubSignature($data, $header_signature)) {
// Process your data
} else {
// return a 401 record.
}
}
}
$token = '';
$secretKey = '';
$hooks = new Hooks($secretKey, $token);
// Call this _GET
$hooks->verify();
// call this _POST
$hooks->process() ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment