为什么我的重新回复不成功?

时间:2017-01-04 10:30:26

标签: php forms recaptcha

我一直在寻找我可能做错的事情和解决方案,但似乎没有一个能解决我的问题。我有一个带recaptcha的表单,非常简单。但是我的recaptcha验证每次都失败了,当它应该成功时,回声“不成功”。我究竟做错了什么?这是我的代码。

<?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;
}
?>

1 个答案:

答案 0 :(得分:1)

//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 [&#39; Response&#39;]更改为响应变量的名称,那么它应该可以正常工作。