Ada*_*urk 5 twilio twilio-php twilio-api php-7.2
所以我真的不知道问题出在哪里,我尝试了很多事情,但是我无法使Twilio请求哈希匹配。让我解释。
我决定实现Twilio的RequestValidator的实例,以确保请求来自Twilio。但是在这里按照本教程进行操作之后:https : //www.twilio.com/docs/usage/security?code-sample=code-validate-signature-of-request-1&code-language=PHP&code-sdk-version=5.x
验证器仅返回false。这是我使用的代码:
$url = 'https://example.com/api/endpoint/to/endpoint/';
$request_params = $_REQUEST;
$twilio_validator = new RequestValidator('myauthtoken');
if (!$twilio_validator->validate($_SERVER['HTTP_X_TWILIO_SIGNATURE'], $url, $request_params)) {
throw new CallException('Not from Twilio');
}
Run Code Online (Sandbox Code Playgroud)
即使该URL是一个示例,这也正是我格式化实际URL的方式……没有端口,基本身份验证或片段。仅协议,域和路径后缀“ /”。此外,URL是我设置此Twilio应用程序时设置的确切VoiceURL(这是将VoiceURL调用到我的Twilio应用程序之一)。
我的身份验证令牌是我整个帐户的身份验证令牌
请求参数是我确定要弄乱东西的地方。Twilio正在对此端点发出GET请求,我也尝试使用$_GET超全局变量,但无济于事。$_REQUEST由于以下问题,我在这里使用:https : //github.com/twilio/twilio-php/issues/510,因为我认为这将是最佳选择。我也尝试过使用file_get_contents('php://input')完全相同的问题(哈希值最终不匹配)。
我什至分叉并在PHP SDK上打开了PR,以更新该类,只是看我是否还能学习更多...所以我知道该类及其方法非常好...我只是看不到我的问题。
为了使RequestValidator无法验证Twilio的请求是否来自Twilio,我在这里做错了什么?
因此,经过大量研究并在 Twilio 的帮助下,我找到了问题的答案。
当 Twilio 向我的服务器发出 GET 请求时,您不应该将 GET 参数作为第三个参数传递给 RequestValidator 类上的 validate 方法。当 Twilio 向服务器发出 GET 请求时,验证实际上需要如下所示:
// this is the interesting part...you don't even set the pathname on the domain...
// EVEN IF YOU THE PATHNAME IS SET IN YOUR VOICE URL.
// This is because of the different way the RequestValidator handles GET and POST params
$domain = 'https://example.com'; // make sure to add no trailing '/'
// setting up the RequestValidator
$twilio_validator = new RequestValidator('myauthtoken');
// figuring out if the request is from twilio
$is_from_twilio = $twilio_validator->validate(
// the signature header that Twilio sends
$_SERVER['HTTP_X_TWILIO_SIGNATURE'],
// The domain name CONCATENATED to the Request URI. $_SERVER['REQUEST_URI'] holds everything that comes after the domain name in a URL (pathname, query parameters, and fragment)
$domain.$_SERVER['REQUEST_URI']
// if the request is a get request, as mine are, there is no need for the third parameter
);
// resolving the response
if (!$is_from_twilio) {
echo 'Not from Twilio';
exit;
}
Run Code Online (Sandbox Code Playgroud)
请参阅代码中的注释,以获取有关此处工作代码的更深入讨论。
| 归档时间: |
|
| 查看次数: |
92 次 |
| 最近记录: |