kamotelab/php-turnstile

PHP implementation of cloudflare's turnstile

v1.2.1 2025-03-12 10:01 UTC

This package is auto-updated.

Last update: 2025-04-12 10:11:18 UTC


README

GitHub Actions Workflow Status GitHub Release

A lightweight PHP implementation of Cloudflare's Turnstile

Requirements

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