Skip to content

Instantly share code, notes, and snippets.

@rutoru
Last active August 29, 2015 14:01
Show Gist options
  • Save rutoru/392a95684b229845a656 to your computer and use it in GitHub Desktop.
Save rutoru/392a95684b229845a656 to your computer and use it in GitHub Desktop.
An example of validating Twilio Request with PHP micro framework Slim.
<?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;
}
}
@rutoru
Copy link
Author

rutoru commented May 13, 2014

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:

<?php
namespace Sample;

require '../vendor/autoload.php'; 

$app = \Slim\Slim::getInstance();
$params = $app->request->params();

$result = validateTwilioRequest($app, $params);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment