PHP code example of oh86 / laravel-captcha-tencentcloud

1. Go to this page and download the library: Download oh86/laravel-captcha-tencentcloud 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/ */

    

oh86 / laravel-captcha-tencentcloud example snippets


return [
    'default' => env('CAPTCHA_DEFAULT_DRIVER', 'tencentCloud'),

    // ...

    // 腾讯云验证码配置
    'tencentCloud' => [
        'secret_id' => env('TENCENT_CLOUD_SECRET_ID'),
        'secret_key' => env('TENCENT_CLOUD_SECRET_KEY'),
        'region' => env('TENCENT_CLOUD_REGION', 'ap-guangzhou'),
        'captcha_app' => [
            'app_id' => (int) env('TENCENT_CLOUD_CAPTCHA_APP_ID'),
            'secret_key' => env('TENCENT_CLOUD_CAPTCHA_SECRET_KEY'),
        ],
    ],
];

use Oh86\Captcha\Facades\Captcha;

// 验证
/** @var bool */
$result = Captcha::driver('tencentCloud')->verify(['ticket' => 'your_ticket_here', 'randStr' => 'your_rand_str_here', 'userIp' => 'your_user_ip_here']);

use App\Constants\ErrorCode;
use Illuminate\Http\Request;
use Oh86\Captcha\Facades\Captcha;
use Oh86\Http\Exceptions\ErrorCodeException;

trait CaptchaTrait
{
    /**
     * 校验验证码
     * @param \Illuminate\Http\Request $request
     * @throws \Oh86\Http\Exceptions\ErrorCodeException
     */
    protected function verifyCaptcha(Request $request)
    {
        $request->validate([
            'captcha_type' => 'nullable',
            'captcha' => ';

            if (!Captcha::driver('image')->driver($request->captcha['type'] ?? null)->verify($request->captcha)) {
                throw new ErrorCodeException(ErrorCode::Error, '验证码错误');
            }
            return;
        } elseif ($captchaType == 'tencentCloud') {
            $request->validate([
                'captcha.ticket' => '