Invisible recaptcha siteverify - 错误代码

时间:2017-06-15 06:19:22

标签: php recaptcha

我正在尝试在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,这是正确的。

1 个答案:

答案 0 :(得分:0)

我从来没有见过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.'));
    }
}