我正在使用此包裝:https://github.com/RobThree/TwoFactorAuth,我正在嘗試遵循指南中您可以使用自己的QR碼提供程序的部分。找不到類別PHP(2FA包裝)
我下載了phpqrcode.php
文件,並將其放置在TwoFactorAuth.php
所在的目錄下。
當require_once
是像指南中的頂部,我得到的錯誤:
Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in /var/www/public/vendor/robthree/twofactorauth/lib/Providers/Qr/MyProvider.php on line 4
因此,與TwoFactorAuth.php
目錄我加myprovider.php
用下面的代碼:
<?php
namespace RobThree\Auth\Providers\Qr;
require_once(__DIR__ . '/../../phpqrcode.php');
class MyProvider implements IQRCodeProvider {
public function getMimeType() {
return 'image/png'; // This provider only returns PNG's
}
public function getQRCodeImage($qrtext, $size) {
ob_start(); // 'Catch' QRCode's output
QRCode::png($qrtext, null, QR_ECLEVEL_L, 3, 4); // We ignore $size and set it to 3
// since phpqrcode doesn't support
// a size in pixels...
$result = ob_get_contents(); // 'Catch' QRCode's output
ob_end_clean(); // Cleanup
return $result; // Return image
}
}
我然後使用以下代碼嘗試生成QR碼,與自述文件類似:
<?php
require_once __DIR__ . '/vendor/autoload.php';
$mp = new RobThree\Auth\Providers\Qr\MyProvider();
$tfa = new RobThree\Auth\TwoFactorAuth('My Company', 6, 30, 'sha1', $mp);
$secret = $tfa->createSecret();
echo $tfa->getQRCodeImageAsDataUri('Bob Ross', $secret);
?>
但後來我得到這個錯誤..
Fatal error: Uncaught Error: Class 'RobThree\Auth\Providers\Qr\QRCode' not found in /var/www/public/vendor/robthree/twofactorauth/lib/Providers/Qr/MyProvider.php:13 Stack trace: #0 /var/www/public/vendor/robthree/twofactorauth/lib/TwoFactorAuth.php(146): RobThree\Auth\Providers\Qr\MyProvider->getQRCodeImage('otpauth://totp/...', 200) #1 /var/www/public/test.php(6): RobThree\Auth\TwoFactorAuth->getQRCodeImageAsDataUri('Bob Ross', 'ID2Y3P5C6N2NXKD...') #2 {main} thrown in /var/www/public/vendor/robthree/twofactorauth/lib/Providers/Qr/MyProvider.php on line 13
誰能幫助我?