ket-php/turnstile

KetPHP-Turnstile is a PHP library for seamless integration of the Cloudflare Turnstile bot protection system. It provides an object-oriented interface for token verification and detailed analysis of API responses.

Maintainers

Package info

github.com/mikhno351/KetPHP-Turnstile

pkg:composer/ket-php/turnstile

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0 2026-03-13 12:46 UTC

This package is auto-updated.

Last update: 2026-03-13 12:51:13 UTC


README

Packagist Version Packagist Downloads Static Badge

Installation

Install via Composer:

composer require ket-php/turnstile

Usage

Basic token verification:

use KetPHP\Turnstile\Turnstile;

$secretKey = '<YOUR_SECRET_KEY>';
$token = $_POST['cf-turnstile-response'];

$turnstile = new Turnstile($secretKey);
if ($turnstile->verify($token)) {
    echo 'Valid!';
} else {
    echo 'Invalid.';
    print_r($turnstile->getResponse()->getErrorCodes());
}

// Inline variant
echo Turnstile::newInstance($secretKey)->verify($token) ? 'Valid' : 'Invalid';

Detailed response processing:

use KetPHP\Turnstile\Turnstile;

$secretKey = '<YOUR_SECRET_KEY>';
$token = $_POST['cf-turnstile-response'];

$turnstile = new Turnstile($secretKey);

$turnstile->verify($token, $_SERVER['REMOTE_ADDR']);

$response = $turnstile->getResponse();

if ($response->isSuccess()) {
    echo 'Host: ' . $response->getHostName();
    echo 'Time: ' . $response->getChallengeAt()->format('Y-m-d H:i:s');
    
    // Checking whether the test key has been used
    if ($response->getMetaData()->isTest()) {
        echo 'Is test request.';
    }
} else {
    foreach ($response->getErrorCodes() as $code) {
        echo 'Description: ' . \KetPHP\Turnstile\ErrorCode::getDescription($code);
        echo 'Action: ' . \KetPHP\Turnstile\ErrorCode::getAction($code);
    }
}