【发布时间】:2021-07-22 20:38:04
【问题描述】:
我正在使用 Grav 创建网站。我使用 Zammad 作为票务系统,并希望在页面上包含反馈表。为此,我使用 Zammad 的 API“https://admin-docs.zammad.org/en/latest/channels/form.html”。这是可行的。可以通过表单创建新票证。现在我想添加一个机器人保护。为此,我选择了 hCaptcha。 https://docs.hcaptcha.com/(Google reCaptcha 可以通过现成的插件“Form”轻松使用,但我不想使用 Google reCaptcha)。我也已经开始写hCaptcha的插件了,但是找不到Grav的API入口。
我当前的代码:
function onFormProcessed(Event $event){
if(is_entered_data_valid()) {
if(isset($_POST['h-captcha-response']) && !empty($_POST['h-captcha-response'])){
$secret = "0x0000000000000000000000000000000000000000";
$remote_address = $_SERVER['REMOTE_ADDR'];
$verify_url = "https://hcaptcha.com/siteverify?secret=".$secret."&response=".$_POST['h-captcha-response']."&remoteip=".$remote_address;
// This is hcaptcha url
$response = file_get_contents($verify_url); # Get token from post data with key 'h-captcha-response' and Make a POST request with data payload to hCaptcha API endpoint
$responseData = json_decode($response);
$success_msg="";
$err_msg="";
if($responseData->success){
$success_msg = "You can process your login functionality";
}else{
$err_msg = "Something went wrong while hCaptcha Validation. Please try again after sometime.";
}
}else{
$err_msg = "Please fill all the required fields";
}
} else {
// Server side validation failed
$error_output = "Please fill all the required fields";
}
// Get the response and pass it into your ajax as a response.
$return_msg = array(
'error' => $err_msg,
'success' => $success_msg
);
echo json_encode($return_msg);
}
这个函数必须在表单提交时执行
【问题讨论】:
-
"但是我找不到 Grav 的 API 的正确入口"...你在找什么样的 "入口"?
-
我正在寻找在提交表单时由 Grav API 执行的函数
-
提交表单时触发事件'onFormProcessed'。查看learn.getgrav.org/17/forms/forms/reference-form-actions,尤其是“自定义操作”部分。看看这是否给你一个线索。