Created
June 29, 2017 16:08
-
-
Save jcipriano/1db0e8d3e0347fa1149982d7a3b3d508 to your computer and use it in GitHub Desktop.
PHP example for generating challenge response for Twitter webhooks
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 | |
// 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