Recommend this page to a friend! |
![]() |
Info | Documentation | ![]() |
![]() |
![]() |
Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2025-01-23 (2 months ago) ![]() | Not yet rated by the users | Total: Not yet counted | Not yet ranked |
Version | License | PHP version | Categories | |||
cloudflare-turnstile 1.0 | MIT/X Consortium ... | 5 | Web services, Validation, PHP 7 |
Description | Author | |
This package can verify human users with Cloudflare Turnstile. |
|
ericsizemore/cloudflare-turnstile
- A PHP library for server-side validation of Cloudflare Turnstile challenges. This library is PSR-18 compatible and framework-agnostic.
> [!IMPORTANT] > WIP: This library is not yet finished. Not recommended for production yet.
>[!NOTE] > This library requires additional libraries to work successfully. Please see below.
---
This library is decoupled from any HTTP messaging client by using PSR-7, PSR-17, and PSR-18.
You can install the package via composer:
# First, install the base package
composer require esi/cloudflare-turnstile
# Then install your preferred PSR implementations. See 'PSR Implementation Libraries' below. For example:
# Option 1: Using Symfony components (recommended for Symfony projects)
composer require symfony/http-client:^7.0 symfony/psr-http-message-bridge:^7.0 nyholm/psr7:^1.0
# Option 2: Using Guzzle (recommended for Laravel projects)
composer require guzzlehttp/guzzle:^7.0
# Option 3: Using Laminas
composer require laminas/laminas-diactoros:^3.0 php-http/curl-client:^2.0
# Option 4: Using PHPHttp
composer require nyholm/psr7:^1.0 php-http/curl-client:^2.0
# Option 5: Using Buzz
composer require kriswallsmith/buzz:^1.3 nyholm/psr7:^1.0
# There are various combinations. Guzzle is all in one, while there are various combinations between Symfony, Laminas, PHPHttp, NyHolm, etc.
Below are some recommended libraries that implement the required PSR interfaces. You'll need one implementation of each PSR to use this library.
HTTP message and URI interface implementations:
Factory interfaces for PSR-7:
HTTP Client implementations:
| PSR-18 HTTP Client | PSR-7/17 Implementation | Additional Requirements | |---------------------|-------------------------|-------------------------| | Guzzle | Built-in | None | | Symfony HTTP Client | Nyholm PSR-7 | psr-http-message-bridge | | PHP-HTTP Curl | Any PSR-7/17 | None | | Buzz | Any PSR-7/17 | None | | Socket Client | Any PSR-7/17 | None |
Using Symfony components:
composer require esi/cloudflare-turnstile symfony/http-client:^7.0 symfony/psr-http-message-bridge:^7.0 nyholm/psr7:^1.0
Using Guzzle:
composer require esi/cloudflare-turnstile guzzlehttp/guzzle:^7.0
Using Laminas:
composer require esi/cloudflare-turnstile laminas/laminas-diactoros:^3.0 php-http/curl-client:^2.0
use Esi\CloudflareTurnstile\Turnstile;
use Esi\CloudflareTurnstile\Exceptions\ValueObjectInvalidValueException;
use Esi\CloudflareTurnstile\ValueObjects\SecretKey;
use Esi\CloudflareTurnstile\ValueObjects\Token;
use Esi\CloudflareTurnstile\VerifyConfiguration;
use Psr\Http\Client\ClientExceptionInterface;
/
* // Using Guzzle
* use GuzzleHttp\Client;
* use GuzzleHttp\Psr7\HttpFactory;
*
* $client = new Client();
* $factory = new HttpFactory();
* $turnstile = new Turnstile($client, $factory, $factory, new SecretKey('your-secret-key'));
*
* // Using Symfony HTTP Client
* use Symfony\Component\HttpClient\Psr18Client;
* use Nyholm\Psr7\Factory\Psr17Factory;
*
* $client = new Psr18Client();
* $factory = new Psr17Factory();
* $turnstile = new Turnstile($client, $factory, $factory, new SecretKey('your-secret-key'));
*
* // Using PHP-HTTP Curl Client
* use Http\Client\Curl\Client;
* use Nyholm\Psr7\Factory\Psr17Factory;
*
* $factory = new Psr17Factory();
* $client = new Client();
* $turnstile = new Turnstile($client, $factory, $factory, new SecretKey('your-secret-key'));
*/
// Initialize with your preferred PSR-18 client and PSR-17 factories
$httpClient = new \Your\Preferred\HttpClient();
$requestFactory = new \Your\Preferred\RequestFactory();
$streamFactory = new \Your\Preferred\StreamFactory();
try {
// Create the Turnstile client
$turnstile = new Turnstile(
$httpClient,
$requestFactory,
$streamFactory,
new SecretKey('your-secret-key')
);
// Create configuration with the response token from the frontend
$config = new VerifyConfiguration(
new Token('response-token-from-widget')
);
$response = $turnstile->verify($config);
if ($response->isSuccess()) {
// Verification successful
echo 'Challenge passed!';
} else {
// Verification failed
echo 'Challenge failed: ' . implode(', ', $response->getErrorCodes());
}
} catch (ValueObjectInvalidValueException $e) {
// Handle an invalid value being passed to Token, IpAddress, or SecretKey
echo 'Config Error: ' . $e->getMessage();
} catch (\RuntimeException $e) {
// Handle JSON decode errors
echo 'Error: ' . $e->getMessage();
} catch (ClientExceptionInterface $e) {
// Handle HTTP client errors
echo 'HTTP Error: ' . $e->getMessage();
}
use Esi\CloudflareTurnstile\Turnstile;
use Esi\CloudflareTurnstile\Exceptions\ValueObjectInvalidValueException;
use Esi\CloudflareTurnstile\ValueObjects\IdempotencyKey;
use Esi\CloudflareTurnstile\ValueObjects\IpAddress;
use Esi\CloudflareTurnstile\ValueObjects\SecretKey;
use Esi\CloudflareTurnstile\ValueObjects\Token;
use Esi\CloudflareTurnstile\VerifyConfiguration;
// Initialize with your preferred PSR-18 client and PSR-17 factories
$httpClient = new \Your\Preferred\HttpClient();
$requestFactory = new \Your\Preferred\RequestFactory();
$streamFactory = new \Your\Preferred\StreamFactory();
try {
// Create the Turnstile client
$turnstile = new Turnstile(
$httpClient,
$requestFactory,
$streamFactory,
new SecretKey('your-secret-key')
);
// Create configuration with all available options
$config = new VerifyConfiguration(
new Token('response-token-from-widget'),
new IpAddress('127.0.0.1'), // Optional: Client IP address
new IdempotencyKey('unique-request-id'), // Optional: Idempotency key
[ // Optional: Custom data
'action' => 'login',
'cdata' => 'custom-verification-data'
]
);
$response = $turnstile->verify($config);
if ($response->isSuccess()) {
// Verification successful
echo 'Challenge passed!';
} else {
// Verification failed
echo 'Challenge failed: ' . implode(', ', $response->getErrorCodes());
}
} catch (ValueObjectInvalidValueException $e) {
// Handle an invalid value being passed to Token, IpAddress, or SecretKey
echo 'Config Error: ' . $e->getMessage();
} catch (\RuntimeException $e) {
// Handle JSON decode errors
echo 'Error: ' . $e->getMessage();
} catch (ClientExceptionInterface $e) {
// Handle HTTP client errors
echo 'HTTP Error: ' . $e->getMessage();
}
The response object provides several methods to access verification details:
$response = $turnstile->verify($config);
// Basic verification status
$success = $response->isSuccess();
// Timestamp of the challenge
$timestamp = $response->getChallengeTs();
// Hostname where the challenge was solved
$hostname = $response->getHostname();
// Any error codes returned
$errorCodes = $response->getErrorCodes();
// Optional action name (if set in widget)
$action = $response->getAction();
// Optional custom data (if provided)
$customData = $response->getCdata();
// Enterprise only: metadata
$metadata = $response->getMetadata();
// Access the raw response data
$rawData = $response->getRawData();
See docs/laravel.md and docs/symfony.md.
See docs/faq.md.
See docs/performance.md.
See CONTRIBUTING.
Bugs and feature requests are tracked on GitHub.
See backward-compatibility.md for more information on Backwards Compatibility.
See the CHANGELOG for more information on what has changed recently.
See the LICENSE for more information on the license that applies to this project.
See TODO for more information on what is planned for future releases.
See SECURITY for more information on the security disclosure process.
![]() |
File | Role | Description | ||
---|---|---|---|---|
![]() |
||||
![]() |
||||
![]() |
||||
![]() |
||||
![]() ![]() |
Example | Example script | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Lic. | License text | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Doc. | Documentation | ||
![]() |
Class | Class source | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
![]() |
![]() | cloudflare-turnstile-2025-01-23.zip 81KB |
![]() | cloudflare-turnstile-2025-01-23.tar.gz 63KB |
![]() | Install with Composer |
Version Control | Unique User Downloads | |||||||
100% |
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.