NoCaptcha returning error invalid-json

2020-05-23 02:05发布

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.

7条回答
看我几分像从前
2楼-- · 2020-05-23 02:43

use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;
use ReCaptcha\RequestMethod\CurlPost;

class NotSSLCurl extends CurlPost implements RequestMethod
{
    public function __construct()
    {
        $this->curl = curl_init(self::SITE_VERIFY_URL);
        curl_setopt($this->curl, CURLINFO_HEADER_OUT, 0);
        curl_setopt($this->curl, CURLOPT_HEADER, 0);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->curl, CURLOPT_BINARYTRANSFER,1);
        curl_setopt($this->curl, CURLOPT_TIMEOUT, 9999);
        curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0');
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($this->curl, CURLOPT_POST , 1);
        curl_setopt($this->curl, CURLOPT_HTTPHEADER , array('Content-Type: application/x-www-form-urlencoded'));
    }

    public function submit(RequestParameters $params)
    {
        curl_setopt($this->curl, CURLOPT_POSTFIELDS , $params->toQueryString());
        return curl_exec($this->curl);
    }

    public function __destruct()
    {
        curl_close($this->curl);
    }
}

// example validation:
$recaptcha = new ReCaptcha('CLIENT_SECRECT', new NotSSLCurl());
$resp      = $recaptcha->verify(@$_POST['g-recaptcha-response'],$_SERVER['REMOTE_ADDR']);
var_dump($resp->isSuccess());
查看更多
淡お忘
3楼-- · 2020-05-23 02:55

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());
查看更多
够拽才男人
4楼-- · 2020-05-23 02:56

the response is a json object. it should be decoded first. i am pretty new to web programming/development, but i successfully integrated google recaptcha on an asp.net test site which does nothing for now, but i'm sure it handles the json response just the way i want it to..

found another, could this help.

查看更多
smile是对你的礼貌
5楼-- · 2020-05-23 03:02

This solved it for me:

//$recaptcha = new \ReCaptcha\ReCaptcha($secret);

// If file_get_contents() is locked down on your PHP installation to disallow
// its use with URLs, then you can use the alternative request method instead.
// This makes use of fsockopen() instead.
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\SocketPost());
查看更多
我只想做你的唯一
6楼-- · 2020-05-23 03:03

// Make the call to verify the response and also pass the user's IP address

  $resp = $recaptcha->verify($recaptcha_response, $this->CI->input->ip_address());
查看更多
再贱就再见
7楼-- · 2020-05-23 03:04

If you are using reCAPTCHA PHP client library < 1.2.4 with SocketPost request method then you need to update to ≤ 1.2.4 or you should switch to CurlPost because the Google server response has changed: https://github.com/google/recaptcha/issues/359

查看更多
登录 后发表回答