Last active
August 29, 2015 14:01
-
-
Save rutoru/392a95684b229845a656 to your computer and use it in GitHub Desktop.
An example of validating Twilio Request with PHP micro framework Slim.
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 | |
/** | |
* validateTwilioRequest | |
* | |
* @param \Slim\Slim $app Slim Object | |
* @param Array $params Input parameters | |
* @return Boolean Result of the validation | |
* @license http://www.opensource.org/licenses/mit-license.php 2014 rutoru | |
*/ | |
namespace Sample; | |
function validateTwilioRequest($app, $params){ | |
// Get the request. | |
$req = $app->request; | |
// Get the X-Twilio-Signature header. | |
$twilioSignature = $req->headers->get('HTTP_X_TWILIO_SIGNATURE'); | |
// Get the Twilio request URL. | |
$url = $req->getUrl().$req->getPath(); | |
// Create Validator | |
$validator = new \Services_Twilio_RequestValidator("Your auth token from twilio.com/user/account"); | |
// Validate | |
// *** Important *** Validation needs all parameter, so you have to pass $params to the validate method. | |
if ($validator->validate($twilioSignature, $url, $params)) { | |
return true; | |
}else{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://twilio-php.readthedocs.org/en/latest/usage/validation.html
The object "\Services_Twilio_RequestValidator" needs three parameters, $twilioSignature, $url and $params.
Twill Request Validator creates a hash value from all post parameters, so you need to pass $param to the $validator->validate() method with no change.
You can call "validateTwilioRequest" function by this: