I integrated Googles funky ReCaptcha NoCaptcha into a simple html5 form. On localhost it is working, but testing online it always returns the error 'invalid-json'. Here is part of my code:
$secret = 'TEHSEHCRET'; $recaptcha = new \ReCaptcha\ReCaptcha($secret); $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); if ($resp->isSuccess()) { // do some } else { print_r($errors = $resp->getErrorCodes()); }
Returns Array ( [0] => invalid-json )
I googled for some help but found nothing really helpful.
Since the code on- and offline is the same I am really clueless where the problem is coming from. https://developers.google.com/recaptcha/docs/verify says nothing about the error code. Guess the solution is too simple.
I had the same issue and fixed it by using cURL as request method instead POST.
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\CurlPost());
The key to the solution was to simply turn on the php errors (I know, it's embarrassing). This delivered the error that kept me struggling and also delivered the solution at the same time:
PHP had problems connecting to the https verifying page of google. That was just because of a single option in the php.ini: allow_url_fopen
php.net description:
allow_url_fopen boolean
This option enables the URL-aware fopen wrappers that enable accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers.
Changing its value from 0 to 1 solved my problem. Shows even more how important it is to turn on php errors when developing (I am a super noob to php programming)
Hope this helps somebody some time!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With