【问题标题】:Invisible recaptcha siteverify - error codes不可见的recaptcha siteverify - 错误代码
【发布时间】:2017-06-15 06:19:21
【问题描述】:

我正在尝试在https://www.google.com/recaptcha/api/siteverify 上验证我的$_POST['g-recaptcha-response'],但我不断得到以下结果:

  "success": false,
  "error-codes": [
    "missing-input-response",
    "missing-input-secret"
  ]

我的代码:

 if($has_errors == false) {
    $result = file_get_contents( 'https://www.google.com/recaptcha/api/siteverify', false, stream_context_create( array(
      'http' => array(
          'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
          'method'  => 'POST',
          'content' => http_build_query( array(
            'response' => $_POST['g-recaptcha-response'],
            'secret'   => variable_get('google_recaptcha_secret', '')
          ) ),
      ),
    ) ) );

    var_dump($result);

    $result = json_decode($result);

    if($result->success == false) {
      form_set_error('name', t('Submission blocked by Google Invisible Captcha.'));
    }
  }

我检查了我的变量google_recaptcha_secret,它是正确的。

【问题讨论】:

    标签: php recaptcha


    【解决方案1】:

    我从未见过 file_get_contents 曾经发布过这样的数据,我并不是说不可能,但我建议尝试使用 cURL:

    if ($has_errors == false) {
        $data = [
            'response' => $_POST['g-recaptcha-response'],
            'secret'   => variable_get('google_recaptcha_secret', ''),
        ];
    
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($curl);
        $error = !$result ? curl_error($curl) : null;
        curl_close($curl);
    
        var_dump($result);
    
        $result = json_decode($result);
    
        if ($error || $result->success == false) {
            form_set_error('name', t('Submission blocked by Google Invisible Captcha.'));
        }
    }
    

    【讨论】:

    • 我在一个在线示例中找到了 file_get_contents 代码,但使用 curl 解决了这个问题 - 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多