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:
| Key | Data type | Remarks |
|---|---|---|
| success | boolean | true when the API returned success, the score is not below the threshold and there is no error |
| errors | array | Error 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
| Name | Data type | Default | Remarks |
|---|---|---|---|
| $response | string | Token posted by the widget (g-recaptcha-response) | |
| $threshold | float | null | Minimum 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']);
}