PHP code example of grizzlyware / yubikey-php

1. Go to this page and download the library: Download grizzlyware/yubikey-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

grizzlyware / yubikey-php example snippets




// These can be obtained from Yubico: https://upgrade.yubico.com/getapikey/
$clientId = 12345; // Replace with your client ID
$clientSecret = 'YOUR_SECRET'; // Replace with your client secret. This can also be null or omitted, but the requests and responses will not be signed

// This will come from your user
$otpToValidate = 'OTP_GENERATED_BY_HARDWARE_YUBIKEY';

// Create the validator instance
$yubiKeyValidator = new \Grizzlyware\YubiKey\Validator($clientId, $clientSecret);

try
{
    // Check the OTP
    $yubiKeyValidator->verifyOtp($otpToValidate);

    // OTP was validated successfully
}
catch(Grizzlyware\YubiKey\Exceptions\Yubico\BadOtpException $e)
{
    // YubiKey failed validation
}
catch(Grizzlyware\YubiKey\Exceptions\Exception $e)
{
    // Other error relating to Yubico validation
}
catch(\Exception $e)
{
    // PHP level exception
}