Recaptcha 丢失输入响应 [英] Recaptcha missing-input-response

查看:59
本文介绍了Recaptcha 丢失输入响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 google reCaptcha 时遇到问题.

I have a problem with google 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] => Array ( [0] => missing-input-response ) )

input of print_r is: Array ( [success] => [error-codes] => Array ( [0] => missing-input-response ) )

如何解决这个问题?

感谢解答

推荐答案

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

Google reCatcha API 您可能需要为 file_get_contents 函数调用指定额外的参数,专门为 SSL 设置上下文选项(如果站点有 SSL).

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).

// 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

这篇关于Recaptcha 丢失输入响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆