2015-10-31 56 views
1

我想在PHP中運行Authy的演示。我已經安裝了Authy庫作曲,所以現在我可以用這樣的硬編碼值註冊用戶:如何通過Authy和PHP通過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('[email protected]', '999-999-9999', 30); // actual credentials omitted 

if($user->ok()){ 
    echo "Success!"; 
    $id = $user->id(); 
    echo($id); 
} 

當上述腳本運行時,確實產生了5位用戶ID,因此所有的似乎是進展順利,但短信從未交付給我的手機。

一個可能的問題可能是我的號碼已被註冊爲應用程序電話(與管理員帳戶相關聯),因此(每個文檔)每個電話號碼必須唯一標識一個用戶,也許我的用戶已經註冊了這個應用程序,因此不需要發送一個新的令牌。如果是這種情況,用戶對象的ID可能是以前註冊的。

但是,其他電話號碼仍存在問題。所以現在我迷路了。

回答

1

事實證明,沒有什麼不對的代碼本身。

這只是Sandbox API實際上並沒有通過發送短信來完成。它僅模擬進程以進行測試,因此身份驗證流程與生產API相同。

當我切換到生產API URL和密鑰時,我能夠在手機上收到短信。

0

Guybrush

有兩件事導致您缺少短信。

  1. 您的代碼示例僅用於註冊用戶。您需要再調用第二個API來發送短信。

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

  • 然而,上述API調用可能不導致一個SMS。您確實正確的是,您使用的手機號碼與您的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('[email protected]', '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")); } 
    

    西蒙

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