繁体   English   中英

如何使用Authy和PHP通过SMS发送用户令牌?

[英]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,因此一切似乎都进行得很好,但是SMS从未发送到我的手机。

一个可能的问题是我的电话号码已经被注册为该应用程序电话(与管理员帐户相关联),因此由于(按文档)每个电话号码都必须唯一地标识一个用户,因此我的电话可能已经为此应用程序注册了,因此无需发送新令牌。 在这种情况下,用户对象的ID可以是先前注册的ID。

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

事实证明,代码本身没有错。

只是Sandbox API实际上并没有通过发送SMS来完成。 它仅模拟用于测试目的的过程,因此身份验证流程与生产API相同。

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

的Guybrush

两件事导致您缺少短信。

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

https://docs.authy.com/totp.html#requesting-sms-codes

  1. 但是,以上API调用可能不会产生SMS。 您确实是正确的,默认情况下,您正在使用的手机号码与Authy应用程序关联的事实不会引起SMS消息。 这里的想法是,由于Authy客户必须在Authy交易之上支付SMS消息的费用,因此那些拥有移动应用程序的用户不需要SMS。

但是,可以忽略此行为。

这样您的最终代码将是:

$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"));    }

西蒙

 $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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM