kamotelab/turnstile

Cloudflare的turnstile的PHP实现

v1.0.0 2024-09-10 09:47 UTC

This package is auto-updated.

Last update: 2024-09-11 09:38:50 UTC


README

GitHub Actions Workflow Status GitHub Release

这是一个轻量级的Cloudflare的turnstile的PHP实现

要求

安装

 composer require kamotelab/turnstile

示例用法

<?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
        }
        
    }
}

免责声明

  • 当响应码为4xx或5xx时,此库不会抛出任何异常
  • 当响应码为4xx或5xx时,您需要实现自己的错误处理
  • 当错误被抛出时,您需要实现自己的错误处理