NoCaptcha 返回错误 invalid-json

NoCaptcha returning error invalid-json

我将 Google 时髦的 ReCaptcha NoCaptcha 集成到一个简单的 html5 表单中。 在本地主机上它工作正常,但在线测试它总是 returns 错误 'invalid-json'。这是我的部分代码:

$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 )

我用谷歌搜索了一些帮助,但没有找到真正有用的东西。

由于在线和离线的代码是一样的,所以我真的不知道问题出在哪里。 https://developers.google.com/recaptcha/docs/verify 没有说明错误代码。猜猜解决方案太简单了。

响应是一个 json 对象。它应该首先被解码。我是网络新手 programming/development,但我在 asp.net 测试站点上成功集成了 google recaptcha,目前该站点什么都不做,但我确定它可以处理 json按照我想要的方式回复..

找到另一个,可以this帮助。

解决方案的关键是简单地打开 php 错误(我知道,这很尴尬)。这带来了让我苦苦挣扎的错误,同时也提供了解决方案:

PHP 连接到 google 的 https 验证页面时出现问题。那只是因为 php.ini 中的一个选项:allow_url_fopen

php.net 描述:

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.

将其值从 0 更改为 1 解决了我的问题。进一步显示在开发时打开 php 错误是多么重要(我是 php 编程的超级菜鸟)

希望这对某些人有所帮助!

我遇到了同样的问题,并通过使用 cURL 作为请求方法来解决它 POST。

$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\CurlPost());

这为我解决了:

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

// 进行调用以验证响应并传递用户的 IP 地址

  $resp = $recaptcha->verify($recaptcha_response, $this->CI->input->ip_address());

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

如果您使用 reCAPTCHA PHP 客户端库 < 1.2.4 和 SocketPost 请求方法,那么您需要更新到 ≥ 1.2.4 或者您应该切换到 CurlPost 因为Google 服务器响应已更改:https://github.com/google/recaptcha/issues/359

我遇到了同样的问题。我通过将 'SocketPost' 替换为 'CurlPost'.

解决了这个问题

已替换

$recaptcha = new \ReCaptcha\ReCaptcha(SECRET_KEY, new \ReCaptcha\RequestMethod\SocketPost());

$recaptcha = new \ReCaptcha\ReCaptcha(SECRET_KEY, new \ReCaptcha\RequestMethod\CurlPost());