A Laravel package that integrates Cloudflare's Turnstile CAPTCHA service.
For Laravel integration:
- PHP 8.2 or higher
- Laravel 9 or higher
For standalone usage:
- PHP 8.2 or higher
- Guzzle HTTP 7.8 or higher
composer require teampanfu/laravel-turnstile
- Add to your
.env
:
TURNSTILE_SITEKEY=1x00000000000000000000AA
TURNSTILE_SECRET=1x0000000000000000000000000000000AA
<form method="POST">
@csrf
<div class="cf-turnstile" data-sitekey="{{ config('turnstile.sitekey') }}"></div>
<button type="submit">Submit</button>
</form>
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
See available configurations for theme, language, etc.
$request->validate([
'cf-turnstile-response' => ['turnstile'],
]);
In lang/[lang]/validation.php
:
'custom' => [
'cf-turnstile-response' => [
'turnstile' => 'Please verify that you are human.',
],
],
The package can also be used without Laravel:
<?php
use Panfu\Laravel\Turnstile\Turnstile;
$turnstile = new Turnstile('your-secret-key');
try {
if ($turnstile->validate($_POST['cf-turnstile-response'])) {
// Verification successful
}
} catch (\RuntimeException $e) {
// Handle validation error (e.g., invalid-input-response)
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle network/request error
}
./vendor/bin/phpunit