Skip to content

ReCaptcha

適用於 Hyperf 的 Google ReCaptcha 元件。

安裝

shell
composer require friendsofhyperf/recaptcha

配置

使用中介軟體或驗證規則前,先發布 config/autoload/recaptcha.php

shell
php bin/hyperf.php vendor:publish friendsofhyperf/recaptcha

透過 RECAPTCHA_SECRET_V2_KEY 配置 reCAPTCHA v2 金鑰,透過 RECAPTCHA_SECRET_V3_KEY 配置 reCAPTCHA v3 金鑰,也可以直接修改釋出檔案中的 v2.secret_keyv3.secret_keydefault 用於選擇未顯式傳入版本時使用的版本。

使用

  • 定義中介軟體
php
namespace App\Middleware;

use FriendsOfHyperf\ReCaptcha\Middleware\ReCaptchaMiddleware;

class V3CaptchaMiddleware extends ReCaptchaMiddleware
{
    protected string $version = 'v3';
    protected string $action = 'register'; 
    protected float $score = 0.35; 
    protected string $hostname; 
}

class V2CaptchaMiddleware extends ReCaptchaMiddleware
{
    protected string $version = 'v2';
    protected string $action = 'register'; 
    protected float $score = 0.35; 
    protected string $hostname; 
}
  • 驗證器使用
php
<?php

namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

class IndexController
{
    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;

    public function foo(RequestInterface $request)
    {
        $validator = $this->validationFactory->make(
            $request->all(),
            [
                'g-recaptcha' => 'required|recaptcha:register,0.34,hostname,v3',
            ],
            [
                'g-recaptcha.required' => 'g-recaptcha is required',
                'g-recaptcha.recaptcha' => 'Google ReCaptcha Verify Fails',
            ]
        );

        if ($validator->fails()){
            // Handle exception
            $errorMessage = $validator->errors()->first();  
        }
        // Do something
    }
}