1

I've been trying to test recaptcha on localhost (xampp with php 5.3); the code below (taken from this answer) works on my remote host and returns 'success':'true', but on localhost { "success": false, "error-codes": [ "missing-input-response" ] } is returned. However, if I change the POST request to a GET request (essentially toggle the comment of the 2 $result = lines), then I get a successful call.

Why does this happen?! Although I'm happy that it works on my actual site, I'd like to understand why GET works but POST doesn't on localhost

<html><head></head><body><script src='https://www.google.com/recaptcha/api.js' async defer>
if(isset($_POST['g-recaptcha-response'])) {
  $url = 'https://www.google.com/recaptcha/api/siteverify';
  $data = array('secret'   => 'my_secret_key',
              'response' => $_POST['g-recaptcha-response']);

  $options = array(
      'http' => array(
      'method'  => "POST",
      'header'  => "Content-type: application/x-www-form-urlencoded" . PHP_EOL,
    'content' => http_build_query($data)
    )
  );

  $context  = stream_context_create($options);
  //$result = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=my_secretkey&response=' . $_POST['g-recaptcha-response']);
  $result = file_get_contents($url, false, $context);
  echo $result;
}
?>

<form method="post" action='recaptchatest.php'>
<input id="test" name="test" />
<div style="left:100px;" class="g-recaptcha" data-sitekey="my_key"></div>
<input type="submit" value="Send" id="submit" class="buttons" />
</form>
</body></html>

For completeness, the output of http_build_query($data) is a correctly formed query string (secret=<my key>&response=<response>) and the output of stream_context_create($options) is Resource id #3

Community
  • 1
  • 1
ChrisW
  • 4,970
  • 7
  • 55
  • 92

0 Answers0