teampanfu / laravel-turnstile
Laravel package for Cloudflare's Turnstile CAPTCHA service
v1.1.0
2025-02-15 06:25 UTC
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.8.2
Requires (Dev)
- phpunit/phpunit: ^10.5.35|^11.3.6|^12.0.1
README
A Laravel package that integrates Cloudflare's Turnstile CAPTCHA service.
Requirements
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
Installation
composer require teampanfu/laravel-turnstile
Laravel Integration
- Add to your
.env
:
TURNSTILE_SITEKEY=1x00000000000000000000AA
TURNSTILE_SECRET=1x0000000000000000000000000000000AA
Add the Widget
<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.
Validate the Response
$request->validate([ 'cf-turnstile-response' => ['turnstile'], ]);
Custom Error Message
In lang/[lang]/validation.php
:
'custom' => [ 'cf-turnstile-response' => [ 'turnstile' => 'Please verify that you are human.', ], ],
Standalone Usage
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 }
Testing
./vendor/bin/phpunit