Recaptcha缺失 - 输入 - 响应

时间:2017-02-19 13:20:08

标签: php recaptcha

大家好,我有谷歌reCaptcha的问题。

这是我的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
}

print_r的输入是:Array([success] => [error-codes] =>数组([0] =>缺少输入 - 响应))

如何解决这个问题?

感谢您的回答

5 个答案:

答案 0 :(得分:8)

请注意:g-recaptcha-respone!= g-recaptcha-response

enter image description here

enter image description here

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 
}

至少在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

你的咖啡馆和路径将是

capath=/etc/ssl/certs/
cafile=/etc/ssl/certs/cacert.pem

答案 1 :(得分:1)

我无法发表评论所以我会在这里回答。我复制了完美的代码。和btw,$ _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));
}

答案 2 :(得分:1)

在我的情况下,我需要在此调用中添加两个额外参数('', '&'):

http_build_query(array(
    'secret' => $secret,
    'response' => $response,
    'remoteip' => $remoteip
), '', '&');

答案 3 :(得分:0)

仅需注意一点,您应该通过POST而不是GET发送所有参数(请参阅https://developers.google.com/recaptcha/docs/verify#api_request)。使用cURL之类的内容来帮助进行请求。

答案 4 :(得分:0)

发生此错误是因为我的页面上有两个ReCaptcha元素实例(一个用于移动视图,一个用于桌面)。一旦我删除其中一个,此错误就停止了。