hyperf-ai / captcha
The Hyperf CAPTCHA package.
v4.0.1
2024-09-05 03:39 UTC
Requires
- php: >=8.0
- ext-fileinfo: *
- ext-imagick: *
- ext-swoole: >=5.0
- hyperf-ai/encryption: ^4.0
- hyperf/cache: ^3.0
- hyperf/di: ^3.0
- hyperf/framework: ^3.0
- hyperf/validation: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- hyperf/testing: ^3.0
- phpstan/phpstan: ^0.12
- swoole/ide-helper: dev-master
This package is not auto-updated.
Last update: 2025-04-03 06:18:44 UTC
README
安装
composer require hyperf-ai/captcha
发布配置
php bin/hyperf.php vendor:publish hyperf-ai/captcha
字体文件默认发布到
<root>/storage/fonts
目录。
组件依赖 hyperf-ai/encryption
组件加解密 key
,依赖 hyperf/cache
组件暂存使用过的 key
,您需要发布这些组件的配置:
php bin/hyperf.php vendor:publish hyperf-ai/encryption
php bin/hyperf.php vendor:publish hyperf/cache
使用
use Hyperf\Utils\ApplicationContext;
use HyperfAi\Captcha\CaptchaFactory;
$captchaFactory = ApplicationContext::getContainer()->get(CaptchaFactory::class);
// 生成
$captcha = $captchaFactory->create();
$response = [
'key' => $captcha->getKey(),
'blob' => $captcha->getBlob()->toDataUrl(),
'ttl' => $captcha->getTtl(),
];
// 验证
$captchaFactory->validate($key, $text);
验证器
您可将 key
与用户输入用逗号 ,
连接后传递给 captcha
验证规则来自动完成验证,验证成功的 key
将被自动暂存到缓存中直至该 key
过期。