我试图在我的网站(用PHP和Wordpress开发)中植入谷歌的reCaptcha V2。
我试图验证用户是否在提交之前检查了此Captcha。
这是我的验证:
<?php
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
$privatekey = $secret;
$captcha = $_POST['g-recaptcha-response'];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => $privatekey,
'response' => $captcha,
'remoteip' => $_SERVER['REMOTE_ADDR']
);
$curlConfig = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $data
);
$ch = curl_init();
curl_setopt_array($ch, $curlConfig);
$response = curl_exec($ch);
curl_close($ch);
$jsonResponse = json_decode($response);
if ($jsonResponse->success == true) {
$succMsg = 'Your contact request have submitted successfully.';
echo "<script>alert(\"OK\")</script>";
}
else {
$errMsg = 'Robot verification failed, please try again.';
echo "<script>alert(\"KO ROBOT\")</script>";
}
}
else{
$errMsg = 'Please click on the reCAPTCHA box.';
echo "<script>alert(\"KO CLICK ON BOX\")</script>";
}
?>
当我重新加载页面时,或者当我提交没有经过验证的验证码时,或者当我检查验证码时,它始终显示:"KO ROBOT"
我也尝试使用"file_get_contents"
而不是curl,但我遇到了SSL错误警告。
感谢。
更新:
当我这样做时:
var_dump($jsonResponse);
我的页面上有这个:
object(stdClass)#4028(2){[&#34; success&#34;] =&gt;布尔(假) [&#34;错误码&#34;] =&GT; array(1){[0] =&gt; string(20)&#34; invalid-input-secret&#34; } }
更新2:
现在我有了这个,在核实我的密钥之后:
object(stdClass)#4028(2){[&#34; success&#34;] =&gt;布尔(假) [&#34;错误码&#34;] =&GT; array(1){[0] =&gt; string(20)&#34; timeout-or-duplicate&#34; } }
答案 0 :(得分:4)
尝试使用这个,只需替换密钥。
<?php
$response = isset($_POST["g-recaptcha-response"]) ? $_POST['g-recaptcha-response'] : null;
$privatekey = "YOUR PRIVATE KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'secret' => $privatekey,
'response' => $response,
'remoteip' => $_SERVER['REMOTE_ADDR']
));
$resp = json_decode(curl_exec($ch));
curl_close($ch);
if ($resp->success) {
} else {
//failed return mess
}
?>