Php Recaptcha错误代码';连接失败';论核查
在实现recaptcha v2时,我在尝试验证recaptcha输入时收到错误代码Php Recaptcha错误代码';连接失败';论核查,php,recaptcha,Php,Recaptcha,在实现recaptcha v2时,我在尝试验证recaptcha输入时收到错误代码“连接失败” 我遵循了这个()教程,因为我发现其他人不走运 require('src/autoload.php'); $siteKey='my key'; $secret='我的钥匙'; $recaptcha=new\recaptcha\recaptcha($secret); $gRecaptchaResponse=$_POST['g-recaptcha-response']; $remoteIp=$\u服务器['
“连接失败”
我遵循了这个()教程,因为我发现其他人不走运
require('src/autoload.php');
$siteKey='my key';
$secret='我的钥匙';
$recaptcha=new\recaptcha\recaptcha($secret);
$gRecaptchaResponse=$_POST['g-recaptcha-response'];
$remoteIp=$\u服务器['REMOTE\u ADDR'];
$recaptchaErrors='';
$resp=$recaptcha->verify($gRecaptchaResponse,$remoteIp);
如果($resp->issucess()){
$error[]=“已工作”;
}否则{
$recaptchaErrors=$resp->getErrorCodes();
foreach($recomptchaerrors作为$err)
{
$error[]=$err;
}
}
我在任何地方都没有找到关于这个错误的任何细节,官方的recaptcha页面上也没有记录。出于测试目的,我编辑了上面的代码片段,但它会发送电子邮件。我在运行node服务器的节点环境中本地工作时也遇到了同样的问题。 如果您试图使用localhost reCaptcha密钥对验证localhost的reCaptcha响应,请改为从live Web服务器(使用相对密钥对)进行验证。 出于某种原因,从localhost发送请求时返回了该错误。
我想这与开发环境有关,但没有进一步调查。在运行node服务器的节点环境中本地工作时,我也遇到了同样的问题。 如果您试图使用localhost reCaptcha密钥对验证localhost的reCaptcha响应,请改为从live Web服务器(使用相对密钥对)进行验证。 出于某种原因,从localhost发送请求时返回了该错误。
我想这与开发环境有关,但没有进一步调查。我在本地主机上尝试在我的网站中包含recaptcha时也遇到了同样的问题,然后我在我的live网站(服务器上)上尝试了这段代码,它成功了,希望这能有所帮助
$secret = 'your server side key from google';
$post_data = http_build_query(
array(
'secret' => $secret,
'response' => $_POST['g-recaptcha-response'],
'remoteip' => $_SERVER['REMOTE_ADDR']));
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data));
$context = stream_context_create($opts);
$response =file_get_contents('https://www.google.com/recaptcha/api/siteverify',false, $context);
$result = json_decode($response);
if($result->success){
echo "Success";
}
if (!$result->success) {
echo "CAPTCHA verification failed.");
}
当我试图在本地主机上的网站中包含recaptcha时,我也遇到了同样的问题,然后我在我的实时网站(服务器上)上尝试了这段代码,它成功了,希望这能有所帮助
$secret = 'your server side key from google';
$post_data = http_build_query(
array(
'secret' => $secret,
'response' => $_POST['g-recaptcha-response'],
'remoteip' => $_SERVER['REMOTE_ADDR']));
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data));
$context = stream_context_create($opts);
$response =file_get_contents('https://www.google.com/recaptcha/api/siteverify',false, $context);
$result = json_decode($response);
if($result->success){
echo "Success";
}
if (!$result->success) {
echo "CAPTCHA verification failed.");
}
如果
php.ini
中的allow\u url\u fopen
为off
,则连接将失败,因为默认情况下Recaptcha
使用file\u get\u contents
访问API。我不会启用此标志,因为它会带来安全风险
我的建议是,如果您安装了phpcurl
模块,请将Recaptcha
与curl
连接使用:
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\CurlPost());
如果
php.ini
中的allow\u url\u fopen
为off
,则连接将失败,因为默认情况下Recaptcha
使用file\u get\u contents
访问API。我不会启用此标志,因为它会带来安全风险
我的建议是,如果您安装了phpcurl
模块,请将Recaptcha
与curl
连接使用:
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\CurlPost());
不确定发生了什么变化,因为我们的表单以前工作过,但是切换到Curl后端为我们解决了问题。不确定发生了什么变化,因为我们的表单以前工作过,但是切换到Curl后端为我们解决了问题。