kamotelab / turnstile
Cloudflare的turnstile的PHP实现
v1.0.0
2024-09-10 09:47 UTC
Requires
- php: >=8.3
- php-http/discovery: ^1.17
- psr/http-client: ^1.0
- psr/http-client-implementation: *
- psr/http-factory-implementation: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.64
- nyholm/psr7: ^1.8
- phpstan/phpstan: ^1.12
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.3
- symfony/http-client: ^7.1
README
这是一个轻量级的Cloudflare的turnstile的PHP实现
要求
- PHP ^8.3
- 任何PSR-17、PSR-18和HTTPlug实现(例如 Symfony HTTP Client)
安装
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时,您需要实现自己的错误处理
- 当错误被抛出时,您需要实现自己的错误处理