簡體   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