maatify / cloudflare-turnstile
PHP非官方的Turnstile,是我们团队所知的maatify.dev Turnstile和Cloudflare Captcha处理器库
1.0.0007
2024-08-10 11:08 UTC
Requires
- php: >=8.0
- ext-curl: *
- maatify/functions: ^1.2
- maatify/json: ^1.1
- maatify/logger: ^1.1
README
安装
composer require maatify/cloudflare-turnstile
用法
<?php /** * Created by Maatify.dev * User: Maatify.dev * Date: 2024-08-05 * Time: 11:48 AM * https://www.Maatify.dev */ use Maatify\Turnstile\TurnstileValidation; require 'vendor/autoload.php'; $secret_key = '1x0000000000000000000000000000000AA'; $turnstile = TurnstileValidation::getInstance($secret_key); // ===== get result in array format $result = $turnstile->getResponse(); // ====== get bool of validation $result = $turnstile->isSuccess(); // ====== using maatify json on error response with json code with die and if success there is no error $turnstile->jsonErrors();
示例
getResponse();
成功示例
Array ( [success] => 1 [error-codes] => Array ( ) [challenge_ts] => 2024-08-07T04:45:23.540Z [hostname] => maatify [action] => [cdata] => [metadata] => stdClass Object ( [interactive] => ) )
错误示例
array ( [success] => [error-codes] => Array ( [0] => timeout-or-duplicate ) [messages] => Array ( ) )
isSuccess();
返回 true || false
jsonErrors();
错误示例
Header 400
Body
- 在验证错误时
{ "success": false, "response": 40002, "var": "captcha", "description": { "success": false, "error-codes": [ "invalid-input-secret" ], "messages": [] }, "more_info": "invalid-input-secret", "error_details": "test:72" }{ "success": false, "response": 40002, "var": "captcha", "description": { "success": false, "error-codes": [ "timeout-or-duplicate" ], "messages": [] }, "more_info": "timeout-or-duplicate", "error_details": "test:72" }
- 在缺少或空
$_POST['cf-turnstile-response']
时{ "success": false, "response": 1000, "var": "cf-turnstile-response", "description": "MISSING Cf-turnstile-response", "more_info": "", "error_details": "" }
从HTML代码中创建
<form action="validate.php" method="POST"> <input name="test" value="test"> <!-- Your other form fields --> <!-- add theme and language --> <div class="cf-turnstile" data-sitekey="__YOUR_SITE_KEY__" data-theme="dark" data-language="ar"></div> <input type="submit" value="Submit"> </form> <script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>