Skip to main content
Version: Fegg 5.x (next)

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 or does not answer with HTTP 200 JSON, success is false and errors has one message asking the visitor to try again later. A warning is written to the application log.

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\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']);
}