所以我在实现Recaptcha v2时遇到了麻烦。
这是我的表单代码
<form action="upload.php" method="POST" enctype="multipart/form-data">
<div id="html_element"></div>
<input type="file" name="file" style="font-family:'PSR';">
<button type="submit" name="submit">Upload</button>
<div class="g-recaptcha" data-sitekey="key" data-theme="dark"></div>
这是代码的recaptcha部分(相同的文件,但之前尝试拆分为2)
$secret = 'the secret';
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=". $secret."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$googleobj = json_decode($response);
echo $googleobj;
$verified = $googleobj->success;
if($verified === true)
{
无论我做什么,$verified总是返回false。回显部分是我尝试调试的部分,但这给了我另一个错误,随后是HTTP错误500。
PHP Recoverable fatal error: Object of class stdClass could not be converted to string
$_POST的Vardump‘g-recaptcha-response’返回
string(334) "03AF6jDqXmI34YXuv1jIkgqHFo7TcjWbOq4-LJ_aTBRyzDld5BytgSe4ck_dolLm3C9CUzxela7LWa7hYJeJfEBONPPsx3ol7Ch-7SY8I9WyAFEy-iiGqwxZBY41gCSw7dfT42doqg-FIxwZweLOsH5YEf8i-L2QgkAJEd_PrWc9m2Uf6ZNbTDqCNr3VFqF8_0I-gS0Rhj9Z5XXwQLC9LeNfSWhI0DkpYNgK-hO4nGfEsaZT0PMlAg9DbHh9CzKDUzPpguVxz1zw0FP8CgwyBd9sgzpR4LfAoPuduGj0Z0wVcqbQ-CTifFtH7kCJuNG6bCDEufYfntj-8L"
如果这是非常简单的东西,我很抱歉,但我是PHP新手
发布于 2019-01-19 01:02:24
您正在尝试echo
一个stdObject。
$googleobj = json_decode($response);
echo $googleobj;
这是行不通的,因为$googleobj
不是一个字符串,而是一个对象。使用echo
命令时,PHP会尝试将echo
'd转换为字符串,但不能对对象执行此操作。
相反,您可以这样做:
$googleobj = json_decode($response);
print_r($googleobj);
或者干脆完全删除echo
行。
发布于 2019-01-19 01:04:59
这对我很有效:
$response = file_get_contents(as you have it);
$googleobj = json_decode($response, TRUE);
$verified = $googleobj['success'];
if($verified === true) {
...
json_decode
中的参数true
使其成为关联数组。所以你可以像访问普通数组一样访问它。有关更多信息,请查看this
https://stackoverflow.com/questions/54258349
复制相似问题