为什么我的 recaptcha 响应不成功?

Why is my recaptcha response unsuccessful?

我一直在寻找我可能做错的地方以及所有的解决方案,但 none 似乎可以解决我的问题。我有一个带有 recaptcha 的表格,它非常简单。但是我的 recaptcha 验证每次都失败,而 echo 的 "Not successful" 当它应该成功时。我究竟做错了什么?这是我的代码。

<?php
require_once('src/autoload.php');
require_once('src/ReCaptcha/ReCaptcha.php');
require_once('src/ReCaptcha/RequestMethod.php');
require_once('src/ReCaptcha/RequestParameters.php');
require_once('src/ReCaptcha/Response.php');
require_once('src/ReCaptcha/RequestMethod/Post.php');
require_once('src/ReCaptcha/RequestMethod/Socket.php');
require_once('src/ReCaptcha/RequestMethod/SocketPost.php');
require_once('src/ReCaptcha/RequestMethod/Curl.php');
require_once('src/ReCaptcha/RequestMethod/CurlPost.php');

$gRecaptchaResponse = $_POST['g-recaptcha-response'];
$remoteIp = $_SERVER['REMOTE_ADDR'];

$SITEKEY = 'XXXX';
$secret = 'XXXX';

$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
if ($resp->isSuccess()) {
    echo "success";
} else {
    $errors = $resp->getErrorCodes();
    echo "not success";
    echo $errors;
}
?>
//reCaptcha
$StrUrl = "https://www.google.com/recaptcha/api/siteverify";
$StrSecretKey = "XXXXXX";

$data = array('secret' => $StrSecretKey, 'response' => $_POST['Response']);

$ch = curl_init($StrUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$Response = curl_exec($ch);
curl_close($ch);

$Robot = json_decode($Response);

$data = 0;

if(isset($Robot->success) AND $Robot->success==true){
   //CODE
}

试试这个。它最初是为 AJAX 调用而设计的,但是如果您将 $_POST['Response'] 更改为响应变量的名称,那么它应该可以工作,希望如此。