PHP code example of ethsam / easy-hcaptcha

1. Go to this page and download the library: Download ethsam/easy-hcaptcha 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/ */

    

ethsam / easy-hcaptcha example snippets


// In your controller — pick your favourite style:

// 1) Form Type (recommended — auto-validated)
$form = $this->createFormBuilder()
    ->add('email', EmailType::class)
    ->add('captcha', HcaptchaType::class)
    ->getForm();

// 2) Validation Constraint on a DTO
class ContactDto {
    #[\Ethsam\EasyHcaptcha\Validator\Constraint\Hcaptcha]
    public ?string $captchaToken = null;
}

// 3) Manual call (full control)
public function submit(Request $r, HcaptchaVerifierInterface $verifier): Response
{
    $result = $verifier->verify($r->request->get('h-captcha-response'), $r->getClientIp());
    if (!$result->isSuccess()) {
        return $this->redirectToRoute('contact');
    }
    // ...
}
ini
HCAPTCHA_SITE_KEY=your_public_site_key
HCAPTCHA_SECRET_KEY=your_secret_key
twig
{# templates/contact.html.twig #}
<head>{{ hcaptcha_script() }}</head>
<form method="post">
    {{ hcaptcha_widget() }}
    <button>Send</button>
</form>