PHP code example of codein / recaptcha-enterprise-bundle

1. Go to this page and download the library: Download codein/recaptcha-enterprise-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codein / recaptcha-enterprise-bundle example snippets


// config/bundles.php
return [
    // ...
    Codein\RecaptchaEnterpriseBundle\CodeinRecaptchaEnterpriseBundle::class => ['all' => true],
];

use Codein\RecaptchaEnterpriseBundle\Form\RecaptchaEnterpriseType;
use Codein\RecaptchaEnterpriseBundle\Validator\RecaptchaEnterprise;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;

final class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('email', EmailType::class)
            ->add('message', TextareaType::class)
            ->add('recaptchaToken', RecaptchaEnterpriseType::class, [
                'action_name' => 'contact', // sent to Google; also matched when validating
                'constraints' => [
                    new RecaptchaEnterprise(
                        minScore: 0.7, // optional
                        actionName: 'contact',
                    ),
                ],
            ]);
    }
}

use Codein\RecaptchaEnterpriseBundle\CaptchaFailure\FinderInterface;

public function __construct(private readonly FinderInterface $captchaFailures) {}

// ...
if ($form->isSubmitted() && !$form->isValid() && $this->captchaFailures->has($form)) {
    $failure = $this->captchaFailures->get($form);
    // react to $failure, then re-render the form
}

if ($failure->isLowScore()) {
    $logger->warning('reCAPTCHA refused {score} on {action}', [
        'score' => $failure->getScore(),
        'action' => $failure->result->action,
    ]);
} elseif ($failure->isUnavailable()) {
    $this->addFlash('warning', 'The captcha service is unreachable, please try again in a moment.');
}

if ($failure->isInvalidToken()) {
    $this->addFlash('warning', match ($failure->getInvalidReason()) {
        InvalidReason::EXPIRED => 'Please tick the checkbox again, it expired.',
        InvalidReason::MISSING => 'Please tick the checkbox.',
        default => 'The captcha could not be verified, please try again.',
    });
}

use Codein\RecaptchaEnterpriseBundle\Verifier\VerifierInterface;

public function __construct(private readonly VerifierInterface $verifier) {}

// ...
$result = $this->verifier->verify($token, 'contact');

$result->success;              // whether the token may be accepted, score aside
$result->valid;                // what Google said about the token itself
$result->score;                // null when the assessment carried no risk analysis
$result->invalidReason;        // an InvalidReason enum case, or null
$result->getInvalidReasonName(); // e.g. "EXPIRED"
$result->error;                // set only when no assessment could be obtained at all
$result->raw;                  // the untouched payload
shell
$ php bin/console assets:install public
shell
$ make update-latest PHP_VERSION=8.4
$ make test PHP_VERSION=8.4