php Recaptcha 丢失输入响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42327694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recaptcha missing-input-response
提问by Forlis
Hi guys i have problem with google reCaptcha.
大家好,我有谷歌 reCaptcha 的问题。
Here is my php code:
这是我的php代码:
$secret = 'SECRET_KEY';
$response = $_POST['g-recaptcha-respone'];
$remoteip = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip";
$result_json = file_get_contents($url);
$resulting = json_decode($result_json, true);
print_r($resulting);
if($resulting['success']) {
//Success
}
input of print_r is: Array ( [success] => [error-codes] => Array ( [0] => missing-input-response ) )
print_r 的输入是: Array ( [success] => [error-codes] => Array ( [0] => missing-input-response ) )
How to solve this problem?
如何解决这个问题呢?
Thanks for answers
感谢您的回答
回答by Akshay Hegde
Please note : g-recaptcha-respone
!= g-recaptcha-response
请注意:g-recaptcha-respone
!=g-recaptcha-response
Google reCatcha API you might need to specify additional parameters to the file_get_contents
function call, setting the context options specifically for SSL (If site has SSL).
Google reCatcha API 您可能需要为file_get_contents
函数调用指定其他参数,专门为 SSL 设置上下文选项(如果站点具有 SSL)。
// If submitted check response
if ($_POST["g-recaptcha-response"]) {
// Input data
$secret = 'SECRET_KEY';
$response = $_POST['g-recaptcha-response'];
$remoteip = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify";
$post_data = http_build_query(
array(
'secret' => $secret,
'response' => $response,
'remoteip' => $remoteip
)
);
$options=array(
// If site has SSL then
'ssl'=>array(
// In my case its /etc/ssl/certs/cacert.pem
'cafile' => '/path/to/cacert.pem',
'verify_peer' => true,
'verify_peer_name' => true,
),
'http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data
)
);
$context = stream_context_create( $options );
$result_json = file_get_contents( $url, false, $context );
$resulting = json_decode($result_json, true);
if($resulting['success']) {
//Success
} else {
// action for no response
}
At least on ubuntu - If site has SSL
至少在 ubuntu 上 - 如果站点有 SSL
cd /usr/local/share/ca-certificates
sudo curl http://curl.haxx.se/ca/cacert.pem -o cacert.crt
sudo update-ca-certificates
sudo update-ca-certificates –fresh
and your cafile and path will be
你的咖啡馆和路径将是
capath=/etc/ssl/certs/
cafile=/etc/ssl/certs/cacert.pem
回答by alen
im not able to comment so im going to answer here. i copied my code which works perfectly. and btw, $_POST['g-recaptcha-respone'], are you sure your inputs name is 'g-recaptcha-respone'?
我无法发表评论,所以我要在这里回答。我复制了我的代码,它完美地工作。顺便说一句,$_POST['g-recaptcha-respone'],你确定你的输入名称是'g-recaptcha-respone'吗?
$secret = 'SECRET-KEY';
$response = $_POST['g-recaptcha-response'];
$ip = $_SERVER['REMOTE_ADDR'];
$dav = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$response."&remoteip=".$ip);
$res = json_decode($dav,true);
if($res['success']) {
die(json_encode(0));
} else {
die(json_encode(1));
}
回答by Marek Skiba
In my case I needed to add two extra parameters ('', '&'
) in this call:
就我而言,我需要'', '&'
在此调用中添加两个额外的参数 ( ):
http_build_query(array(
'secret' => $secret,
'response' => $response,
'remoteip' => $remoteip
), '', '&');
回答by vancy-pants
This error happened to me because I had two instances of the ReCaptcha element on my page (one for mobile views, one for desktop). As soon as I removed one of them this error stopped.
这个错误发生在我身上,因为我的页面上有两个 ReCaptcha 元素实例(一个用于移动视图,一个用于桌面)。一旦我删除了其中一个,这个错误就停止了。
回答by Mike
Just a note on this, you should be sending all your params via POST not GET (see https://developers.google.com/recaptcha/docs/verify#api_request). Use something like cURL to help make the request.
请注意,您应该通过 POST 而不是 GET 发送所有参数(请参阅https://developers.google.com/recaptcha/docs/verify#api_request)。使用类似 cURL 的东西来帮助发出请求。