NoCaptcha返回错误invalid-json

问题描述 投票:19回答:7

我将Google的时髦ReCaptcha NoCaptcha集成到一个简单的html5表单中。在localhost上,它正在运行,但是在线测试总是返回错误“ 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());
}

返回Array ( [0] => invalid-json )

我用Google搜索了一些帮助,但没有发现真正有用的东西。

由于在线和离线代码都相同,所以我对于问题的根源一无所知。 https://developers.google.com/recaptcha/docs/verify对错误代码一言不发。猜猜解决方案太简单了。

php json recaptcha
7个回答
52
投票

我有相同的问题,并通过使用cURL作为请求方法而不是POST来解决。

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

7
投票

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

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

php.net描述:

allow_url_fopen布尔值

此选项将启用URL感知的fopen包装器,从而可以访问URL对象,如文件。提供了默认包装,用于访问使用ftp或http协议的远程文件,某些扩展名为zlib可能会注册其他包装。

将其值从0更改为1解决了我的问题。显示了开发时打开php错误的重要性(我是php编程的超级新手)

希望这可以帮助某些人!


2
投票

这为我解决了问题:

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

0
投票

响应是一个json对象。应该先解码。我是Web编程/开发的新手,但是我成功地将Google Recaptcha集成到了asp.net测试站点上,该站点暂时什么也不做,但是我确信它可以按照我想要的方式处理json响应。

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


0
投票

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


0
投票


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.