Skip to content

Instantly share code, notes, and snippets.

@jcipriano
Created June 29, 2017 16:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jcipriano/1db0e8d3e0347fa1149982d7a3b3d508 to your computer and use it in GitHub Desktop.
Save jcipriano/1db0e8d3e0347fa1149982d7a3b3d508 to your computer and use it in GitHub Desktop.
PHP example for generating challenge response for Twitter webhooks
<?php
// Example app consumer secret found in apps.twitter.com
const APP_CONSUMER_SECRET = 'z3ZX4v7mAAUGykl3EcmkqbartmuW8VFOOzCloLx9Q45P0hLrFu';
// Example token provided by incoming GET request
$token = $_GET['crc_token'];
/**
* Creates a HMAC SHA-256 hash created from the app TOKEN and
* your app Consumer Secret.
* @param token the token provided by the incoming GET request
* @return string
*/
function get_challenge_response($token) {
$hash = hash_hmac('sha256', $token, APP_CONSUMER_SECRET, true);
$response = array(
'response_token' => 'sha256=' . base64_encode($hash)
);
return json_encode($response);
}
// prints result
echo get_challenge_response($token);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment