Skip to main content
Version: Fegg 5.x

ReCAPTCHA::auth

Verify a reCAPTCHA token

Description​

public array ReCAPTCHA::auth( string $response, [ float $threshold = null ] )

Sends the token to https://www.google.com/recaptcha/api/siteverify and returns an array:

KeyData typeRemarks
successbooleantrue when the API returned success, the score is not below the threshold and there is no error
errorsarrayError messages (English for the API error codes; a Japanese message when the score is below the threshold)

When the service cannot be reached, success is false (see When the service cannot be reached).

Arguments​

NameData typeDefaultRemarks
$responsestringToken posted by the widget (g-recaptcha-response)
$thresholdfloatnullMinimum score for this call. null: the threshold given to the constructor

Example​

Controller
use Fegg\Tool\Captcha\ReCAPTCHA;

$recaptcha = new ReCAPTCHA(feggEnv('FEGG_RECAPTCHA_SECRET', ''), 0.5);
$result = $recaptcha->auth($this->in('g-recaptcha-response'));

if (!$result['success']) {
$errorMessage = implode(',', $result['errors']);
}