如何使用 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);  
}

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

一个可能的问题是我的号码已经注册为应用 phone(与管理员帐户相关联),因此(根据文档)每个 phone 号码都必须唯一标识一个用户,也许是我的,已经注册了这个应用程序,因此不需要发送新的令牌。如果是这样的话,用户对象的id可能就是之前注册的那个。

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

Guybrush

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

  1. 您的代码示例仅用于用户注册。然后您需要拨打第二个电话 API 来发送短信。

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

  1. 但是,上述 API 调用可能不会产生短信。您确实是正确的,默认情况下,您使用的手机号码与您的 Authy 应用程序关联的事实不会导致短信。这里的想法是,由于 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"));    }

西蒙

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

只是 Sandbox API 没有真正完成发送 SMS。它只是出于测试目的模拟过程,因此身份验证流程与生产相同API。

当我切换到生产 API URL 和密钥时,我能够在我的 phone.

上收到短信
 $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();
}