在使用 ReCaptcha V3 的 Laravel 中,如何访问从规则返回的 "score"?

In Laravel with ReCaptcha V3, how do I access the "score" returned from a rule?

Laravel 中,我正在设置 Google ReCaptcha V3 现在 returns一个"score"。我已经设法设置了一个验证规则以允许我的表单提交(所有工作),但这只会返回 true 或 false 以通过验证。

我如何根据分数来计算?

我正在使用这个作曲家包来帮助我 - https://github.com/google/recaptcha

这是在我的控制器中(我发送令牌以与服务器进行验证的地方):

// validation
$this->validate( $request, array(
    'g_recaptcha_response' => ['required', 'string', new Captcha()]
));

这是规则:

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use ReCaptcha\ReCaptcha;

class Captcha implements Rule
{

    public function __construct()
    {
        //
    }

    public function passes($attribute, $value)
    {
        $recaptcha = new ReCaptcha('SECRET');
        $response = $recaptcha->verify($value, $_SERVER['REMOTE_ADDR']);
        return $response->isSuccess();
    }

    public function message()
    {
        return 'Are you a robot?';
    }

}

我可以通过控制器以某种方式访问​​ class 吗?我可以在包中看到我需要使用 ->getScore() 但我不知道如何访问它?

正如您在验证规则中所做的那样,您也可以在控制器中获得分数:

public function something(YourRequest $request){

    $recaptcha = new ReCaptcha('SECRET');
    $response = $recaptcha->verify($request->g_recaptcha_response, $request->ip());
    $score =  $response->getScore();
}

可以找到更多可用的响应方法here