如何使用Authy和PHP通过短信发送用户令牌


How do I send a user token via SMS with Authy and PHP?

我正在尝试用PHP运行Authy的演示。我已经用Composer安装了Authy库,所以现在我可以使用如下硬编码值注册用户:

$authy_api = new Authy'AuthyApi('<TESTING API KEY>', 'http://sandbox-api.authy.com'); // actual key omitted -- using the key generated for testing, not the one for production
$user = $authy_api->registerUser('something@something.com', '999-999-9999', 30); // actual credentials omitted
if($user->ok()){
    echo "Success!";
    $id = $user->id();
    echo($id);  
}

当上面的脚本运行时,确实生成了一个5位数的用户id,所以一切似乎都很顺利,但短信从未发送到我的手机。

一个可能的问题是,我的号码已经注册为应用程序电话(与管理员帐户关联),因此(根据文档)每个电话号码都必须唯一标识用户,所以我的号码可能已经注册为该应用程序,因此无需发送新的令牌。如果是这种情况,用户对象的id可能是以前注册的。

然而,其他电话号码仍然存在问题。所以现在我迷路了。

Guybush

有两件事导致你没有短信。

  1. 您的代码示例仅用于用户的注册。然后您需要调用第二个API来发送SMS

https://docs.authy.com/totp.html#requesting-短信编码

  1. 然而,上面的API调用可能不会导致短信。你确实正确,你使用的手机号码与你的Authy应用程序相关联,这在默认情况下不会导致短信消息。这里的想法是,因为Authy的客户必须在Authy交易的基础上支付短信费用,所以那些使用移动应用程序的用户不需要短信

但是,这种行为可以被覆盖。

所以你的最终代码是;

$authy_api = new Authy'AuthyApi('<TESTING API KEY>', 'http://sandbox-api.authy.com'); // actual key omitted -- using the key generated for testing, not the one for production
$user = $authy_api->registerUser('something@something.com', '999-999-9999', 30); // actual credentials omitted
if($user->ok()){  
    echo "Success!";
    $id = $user->id();
    echo($id);  
    // Now send SMS, force sending a message even if the user has the Authy mobile app installed
   $sms = $authy_api->requestSms('authy-id', array("force" => "true"));    }

Simon

事实证明,代码本身没有任何问题。

只是Sandbox API实际上并没有发送SMS,它只是为了测试目的而模拟流程,因此验证流程与生产API相同。

当我切换到生产API URL和密钥时,我可以在手机上收到短信。

 $authy_api = new AuthyApi(AUTHY_API_KEY, "https://api.authy.com");
 $user = $authy_api->registerUser($email, $phone_number, $country_code);   //// Register User
 if($user->ok()) {
     $sms = $authy_api->requestSms($user->id()); //// Send SMS
     return $sms->ok();
}