【问题标题】:Recaptcha missing-input-responseRecaptcha 缺少输入响应
【发布时间】:2017-02-19 13:20:08
【问题描述】:

我对 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 ) )

如何解决这个问题?

感谢解答

【问题讨论】:

    标签: php recaptcha


    【解决方案1】:

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

    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
    

    【讨论】:

    • 我的网站上没有 SSL,那么在 $options=array 中添加了什么?还是进入 $context?
    • @Forlis :看我的更新,你错过了s 吗?在g-recaptcha-response
    • 不知何故我想出了recaptcha_response 并想知道为什么它不起作用 man +1
    【解决方案2】:

    就我而言,我需要在此调用中添加两个额外的参数 ('', '&'):

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

    【讨论】:

    • 感谢您的回复!老实说,我没想到“http_build_query”没有构建正确的查询。
    • 使用我的 PHP (5.4.34) 版本,http_build_query 的结果使用 html 实体 '&'来分隔参数。添加额外的两个参数(第二个不需要,除了允许指定第三个)强制结果用纯“&”分隔参数。 http_build_query 的描述确实说它构建了一个 url 编码的查询,但是 url_encode 的描述说它对非字母数字字符使用 %-encoding,但是有一个很长的注释,得出的结论是最便携的 arg 分隔符是 '& ' .真是一团糟!
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      我无法发表评论,所以我将在这里回答。我复制了完美运行的代码,顺便说一句$_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));
      }
      

      【讨论】:

      • Google 说您应该使用 POST,但实际上 GET 可以正常工作。我刚试过。
      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-07
        • 1970-01-01
        • 2023-02-23
        • 2021-09-21
        • 1970-01-01
        • 2016-06-16
        • 1970-01-01
        相关资源
        最近更新 更多