1. Go to this page and download the library: Download usarise/turnstile library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
usarise / turnstile example snippets
declare(strict_types=1);
t\Psr18Client;
use Turnstile\Error\Messages;
use Turnstile\Turnstile;
// Get real API keys at https://dash.cloudflare.com/?to=/:account/turnstile
$siteKey = '1x00000000000000000000AA'; // Always passes (Dummy Testing)
$secretKey = '1x0000000000000000000000000000000AA'; // Always passes (Dummy Testing)
if ($token = $_POST[Turnstile::RESPONSE_KEY] ?? null) {
$turnstile = new Turnstile(
client: new Psr18Client(),
secretKey: $secretKey,
);
$response = $turnstile->verify(
$token, // The response provided by the Turnstile client-side render on your site.
$_SERVER['HTTP_CF_CONNECTING_IP']
?? $_SERVER['HTTP_X_FORWARDED_FOR']
?? $_SERVER['REMOTE_ADDR'],
);
if ($response->success) {
// Valid token - process form
echo 'Form submission successful!';
// Process your form data here
} else {
// Invalid token - show error
echo 'Verification failed. Please try again.';
// Logging errors to error_log
foreach ($response->errorCodes as $key => $code) {
$error = Messages::DESCRIPTION[$code];
$action = Messages::ACTION_REQUIRED[$code];
if ($response->messages && $response->messages[$key] ?? null) {
$message = $response->messages[$key];
} else {
$message = sprintf('%s. %s', $error, $action);
}
error_log(
sprintf(
'Turnstile validation failed #%d: %s: %s',
$key,
$code,
$message,
),
);
}
}
exit;
}
var_dump((string) $response);
var_dump($response->toArray());
var_dump($response->toArray(strict: true));
use Turnstile\Client\Client;
use Turnstile\Turnstile;
$turnstile = new Turnstile(
client: new Client(...),
secretKey: 'secret key',
idempotencyKey: 'idempotency key',
);
$turnstile = new Turnstile(
client: new Psr18Client(),
secretKey: 'secret key',
idempotencyKey: 'idempotency key',
);
use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Psr7\HttpFactory;
use Turnstile\Client\Client;
$client = new Client(
new GuzzleHttpClient(),
new HttpFactory(),
);
use Symfony\Component\HttpClient\Psr18Client;
use Turnstile\Client\Client;
$client = new Client(
new Psr18Client(),
);
use Symfony\Component\HttpClient\Psr18Client;
$client = new Psr18Client();
use GuzzleHttp\Psr7\HttpFactory;
use Symfony\Component\HttpClient\Psr18Client;
use Turnstile\Client\Client;
$client = new Client(
new Psr18Client(
responseFactory: new HttpFactory(),
),
);
use GuzzleHttp\Psr7\HttpFactory;
use Symfony\Component\HttpClient\Psr18Client;
$client = new Psr18Client(
responseFactory: new HttpFactory(),
);
use Symfony\Component\HttpClient\Psr18Client;
use Turnstile\Client\Client;
$client = new Client(
new Psr18Client(),
);
use Symfony\Component\HttpClient\Psr18Client;
$client = new Psr18Client();
use Http\Client\Curl\Client as CurlClient;
use Nyholm\Psr7\Factory\Psr17Factory;
use Turnstile\Client\Client;
$psr17Factory = new Psr17Factory();
$client = new Client(
httpClient: new CurlClient(
responseFactory: $psr17Factory,
streamFactory: $psr17Factory,
),
requestFactory: $psr17Factory,
);
use Http\Discovery\Psr18Client;
use Turnstile\Client\Client;
$client = new Client(
new Psr18Client(),
);
use Http\Discovery\Psr18Client;
$client = new Psr18Client();
use Turnstile\Client\Client;
use Turnstile\Turnstile;
// Real API keys at https://dash.cloudflare.com/?to=/:account/turnstile
$secretKey = '1x0000000000000000000000000000000AA';
$turnstile = new Turnstile(
client: $client,
secretKey: $secretKey,
);
use Ramsey\Uuid\Uuid;
use Turnstile\Turnstile;
$turnstile = new Turnstile(
client: $client,
secretKey: $secretKey, // The site’s secret key.
idempotencyKey: (string) Uuid::uuid4(), // The UUID to be associated with the response.
);
$response = $turnstile->verify(
$token, // The response that will be associated with the UUID (idempotencyKey)
);
if ($response->success) {
// ...
}
$response = $turnstile->verify(
$token, // The response associated with UUID (idempotencyKey)
);
if ($response->success) {
// ...
}
declare(strict_types=1);
ny\Component\HttpClient\Psr18Client;
use Turnstile\Client\Response;
use Turnstile\Error\{Codes as ErrorCode, Messages};
use Turnstile\Turnstile;
$siteKey = 'your-site-key';
$secretKey = 'your-secret-key';
if ($token = $_POST[Turnstile::RESPONSE_KEY] ?? null) {
$turnstile = new Turnstile(
client: new Psr18Client(),
secretKey: $secretKey, // The site’s secret key.
idempotencyKey: (string) Uuid::uuid4(), // The UUID to be associated with the response.
);
$response = validateWithRetry(
$turnstile,
$token,
);
if ($response->success) {
// Valid token - process form
echo 'Form submission successful!';
// Process your form data here
} else {
// Invalid token - show error
echo 'Verification failed. Please try again.';
// Logging errors to error_log
foreach ($response->errorCodes as $key => $code) {
$error = Messages::DESCRIPTION[$code];
$action = Messages::ACTION_REQUIRED[$code];
if ($response->messages && $response->messages[$key] ?? null) {
$message = $response->messages[$key];
} else {
$message = sprintf('%s. %s', $error, $action);
}
error_log(
sprintf(
'Turnstile validation failed #%d: %s: %s',
$key,
$code,
$message,
),
);
}
}
exit;
}
function validateWithRetry(Turnstile $turnstile, string $token, int $maxRetries = 3): Response {
for ($attempt = 1; $attempt <= $maxRetries; $attempt++) {
try {
$result = $turnstile->verify(
$token,
$_SERVER['HTTP_CF_CONNECTING_IP']
?? $_SERVER['HTTP_X_FORWARDED_FOR']
?? $_SERVER['REMOTE_ADDR'],
);
if ($result->httpResponse->getStatusCode() >= 200 && $result->httpResponse->getStatusCode() < 300) {
return $result;
}
// If this is the last attempt, return the error
if ($attempt === $maxRetries) {
return $result;
}
// Wait before retrying (exponential backoff)
sleep(
2 ** $attempt,
);
} catch (Throwable $throwable) {
return new Response(
success: false,
errorCodes: [ErrorCode::INTERNAL_ERROR],
messages: [$throwable->getMessage()],
);
}
}
}
$response = $turnstile->verify(
token: $_POST[Turnstile::RESPONSE_KEY], // The response provided by the Turnstile client-side render on your site.
);
$response = $turnstile->verify(
token: $_POST[Turnstile::RESPONSE_KEY], // The response provided by the Turnstile client-side render on your site.
remoteIp: $_SERVER['REMOTE_ADDR'], // The visitor’s IP address.
);
$response = $turnstile->verify(
token: $_POST[Turnstile::RESPONSE_KEY], // The response provided by the Turnstile client-side render on your site.
remoteIp: $_SERVER['HTTP_CF_CONNECTING_IP'], // The visitor’s IP address.
);
$response = $turnstile->verify(
...
challengeTimeout: 300, // Number of allowed seconds after the challenge was solved.
expectedHostname: $_SERVER['SERVER_NAME'], // Expected hostname for which the challenge was served.
expectedAction: 'login', // Expected customer widget identifier passed to the widget on the client side.
expectedCdata: 'sessionid-123456789', // Expected customer data passed to the widget on the client side.
);
$response->success
$response->errorCodes
$response->challengeTs
$response->hostname
$response->action
$response->cdata
$response->metadata
$response->messages
$response->httpResponse
(string) $response
$response->toArray()
$response->toArray(strict: true)
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.