kamotelab / php-turnstile
PHP implementation of cloudflare's turnstile
v1.2.1
2025-03-12 10:01 UTC
Requires
- php: >=8.3
- php-http/discovery: ^1.20
- psr/http-client: ^1.0
- psr/http-client-implementation: *
- psr/http-factory-implementation: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.64
- nyholm/psr7: ^1.8
- phpstan/phpstan: ^1.12
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.3
- symfony/http-client: ^7.2
README
A lightweight PHP implementation of Cloudflare's Turnstile
Requirements
- PHP ^8.3
- Any PSR-17, PSR-18 and HTTPlug implementations (ie Symfony HTTP Client)
Installation
composer require kamotelab/php-turnstile
Example Usage
<?php
use KamoteLab\Turnstile;
class YourClass
{
private Turnstile $turnstile
public function __construct(Turnstile $turnstile) {
$this->turnstile = new Turnstile('{Turnstile Secret Key}');
}
/*
* $response (required) = turnstile response from client side render on your site.
* $idempotencyKey (optional) = use this if you need to retry failed request.
* $remoteIp (optional) = The visitor’s IP address.
*/
public function yourMethod(string $response, ?string $idempotencyKey = null, ?string $remoteIp = null) {
try {
$this->turnstile->verify($response, $idempotencyKey, $remoteIp)
} catch (Exception $e) {
// next step in case of exception
}
}
}
Disclaimer
- This library will not throw any exception when the response code is 4xx or 5xx
- You will have to implement your own error handling when response code is 4xx, 5xx
- You will have to implement your own error handling when error was thrown