maatify / google-recaptcha-v2
非官方的 Google Recaptcha V2 for PHP,是我们团队所知的 maatify.dev Google Recaptcha V2 处理器库
1.0.0006
2024-08-10 11:15 UTC
Requires
- php: >=8.0
- ext-curl: *
- maatify/functions: ^1.2
- maatify/json: ^1.1
- maatify/logger: ^1.1
README
安装
composer require maatify/google-recaptcha-v2
用法
<?php /** * Created by Maatify.dev * User: Maatify.dev * Date: 2024-08-08 * Time: 5:19 PM * https://www.Maatify.dev */ use Maatify\GoogleRecaptchaV2\GoogleReCaptchaV2Validation; require 'vendor/autoload.php'; $secret_key = '0x0000000000000000000000000000000000000000'; $google_recaptcha_v2 = GoogleReCaptchaV2Validation::getInstance($secret_key); // ===== if you want to validate domain use $google_recaptcha_v2->setHostname('maatify.dev'); // ===== if you want to validate score (invisible only) $google_recaptcha_v2->setScore(0.5); // ===== if you want to validate action (invisible only) $google_recaptcha_v2->setAction('login'); // ===== get result in array format $result = $google_recaptcha_v2->getResponse(); // ====== get bool of validation $result = $google_recaptcha_v2->isSuccess(); // ====== using maatify json on error response with json code with die and if success there is no error $google_recaptcha_v2->jsonErrors();
示例
getResponse();
成功示例
Array ( [success] => 1 [challenge_ts] => 2024-08-08T14:13:05Z [hostname] => localhost )
错误示例
Array ( [success] => [error-codes] => Array ( [0] => invalid-input-response ) )
Array ( [success] => [error-codes] => Array ( [0] => missing-input-secret ) )
Array ( [success] => [error-codes] => Array ( [0] => bad-request ) )
Array ( [success] => [error-codes] => Array ( [0] => invalid-hostname ) )
Array ( [success] => [error-codes] => Array ( [0] => timeout-or-duplicate ) )
Array ( [success] => [error-codes] => Array ( [0] => invalid-action ) )
Array ( [success] => [error-codes] => Array ( [0] => score-is-low ) )
isSuccess();
返回 true || false
jsonErrors();
错误示例
Header 400
Body
- 在验证错误时
{ "success": false, "response": 40002, "var": "captcha", "description": { "success": false, "error-codes": [ "missing-input-response" ], "messages": [] }, "more_info": "missing-input-response", "error_details": "test:72" }{ "success": false, "response": 40002, "var": "captcha", "description": { "success": false, "error-codes": [ "invalid-hostname" ], "messages": [] }, "more_info": "invalid-hostname", "error_details": "test:72" }{ "success": false, "response": 40002, "var": "captcha", "description": { "success": false, "error-codes": [ "invalid-action" ], "messages": [] }, "more_info": "invalid-action", "error_details": "test:72" }{ "success": false, "response": 40002, "var": "captcha", "description": { "success": false, "error-codes": [ "score-is-low" ], "messages": [] }, "more_info": "score-is-low", "error_details": "test:72" }
- 在缺少或为空的
$_POST['g-recaptcha-response']
时{ "success": false, "response": 1000, "var": "g-recaptcha-response", "description": "MISSING G-recaptcha-response", "more_info": "", "error_details": "" }
在 HTML 代码中创建
可见的 recaptcha
<form action="process.php" method="POST"> <form method="POST"> <!-- Your other form fields --> <div class="g-recaptcha" data-sitekey="__YOUR_SITE_KEY__" data-theme="dark" data-hl="ar"></div> <input type="submit" value="Submit"> </form> <script src="https://www.google.com/recaptcha/api.js?hl=en" async defer></script>
不可见的 recaptcha
<script src="https://www.google.com/recaptcha/api.js?hl=ar"></script> <script> function onSubmit(token) { document.getElementById("demo-form").submit(); } </script> <form method="POST" id="demo-form"> <input name="test" value="test"> <button class="g-recaptcha" data-sitekey="__YOUR_SITE_KEY__" data-callback='onSubmit' data-action='__YOUR_ACTION__'>Submit</button> </form>