NoCaptcha returning error invalid-json

为君一笑 提交于 2020-04-07 14:46:34

问题


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.


回答1:


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());



回答2:


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!




回答3:


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());



回答4:


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.




回答5:


// 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());



回答6:


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());



回答7:


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



来源:https://stackoverflow.com/questions/30106668/nocaptcha-returning-error-invalid-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!