PHP 中的 ReCaptcha v2 实施
ReCaptcha v2 implementation in PHP
所以我在实施 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 总是 returns false。回声部分是我尝试调试,但这给了我另一个错误,然后是 HTTP 错误 500。
PHP Recoverable fatal error: Object of class stdClass could not be converted to string
$_POST 的转储['g-recaptcha-response'] returns
string(334) "03AF6jDqXmI34YXuv1jIkgqHFo7TcjWbOq4-LJ_aTBRyzDld5BytgSe4ck_dolLm3C9CUzxela7LWa7hYJeJfEBONPPsx3ol7Ch-7SY8I9WyAFEy-iiGqwxZBY41gCSw7dfT42doqg-FIxwZweLOsH5YEf8i-L2QgkAJEd_PrWc9m2Uf6ZNbTDqCNr3VFqF8_0I-gS0Rhj9Z5XXwQLC9LeNfSWhI0DkpYNgK-hO4nGfEsaZT0PMlAg9DbHh9CzKDUzPpguVxz1zw0FP8CgwyBd9sgzpR4LfAoPuduGj0Z0wVcqbQ-CTifFtH7kCJuNG6bCDEufYfntj-8L"
对不起,如果这是非常基本的东西,但我是 PHP
的新手
您正在尝试 echo
stdObject。
$googleobj = json_decode($response);
echo $googleobj;
这不起作用,因为 $googleobj
不是字符串,它是对象。使用 echo
命令时,PHP 会尝试将 echo
中的任何内容转换为字符串,而对象无法做到这一点。
相反,您可以这样做:
$googleobj = json_decode($response);
print_r($googleobj);
或者完全删除 echo
行。
这对我有用:
$response = file_get_contents(as you have it);
$googleobj = json_decode($response, TRUE);
$verified = $googleobj['success'];
if($verified === true) {
...
json_decode
中的参数true
使其成为关联数组。所以你可以像普通数组一样访问它。
查看 this 了解更多信息
所以我在实施 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 总是 returns false。回声部分是我尝试调试,但这给了我另一个错误,然后是 HTTP 错误 500。
PHP Recoverable fatal error: Object of class stdClass could not be converted to string
$_POST 的转储['g-recaptcha-response'] returns
string(334) "03AF6jDqXmI34YXuv1jIkgqHFo7TcjWbOq4-LJ_aTBRyzDld5BytgSe4ck_dolLm3C9CUzxela7LWa7hYJeJfEBONPPsx3ol7Ch-7SY8I9WyAFEy-iiGqwxZBY41gCSw7dfT42doqg-FIxwZweLOsH5YEf8i-L2QgkAJEd_PrWc9m2Uf6ZNbTDqCNr3VFqF8_0I-gS0Rhj9Z5XXwQLC9LeNfSWhI0DkpYNgK-hO4nGfEsaZT0PMlAg9DbHh9CzKDUzPpguVxz1zw0FP8CgwyBd9sgzpR4LfAoPuduGj0Z0wVcqbQ-CTifFtH7kCJuNG6bCDEufYfntj-8L"
对不起,如果这是非常基本的东西,但我是 PHP
的新手您正在尝试 echo
stdObject。
$googleobj = json_decode($response);
echo $googleobj;
这不起作用,因为 $googleobj
不是字符串,它是对象。使用 echo
命令时,PHP 会尝试将 echo
中的任何内容转换为字符串,而对象无法做到这一点。
相反,您可以这样做:
$googleobj = json_decode($response);
print_r($googleobj);
或者完全删除 echo
行。
这对我有用:
$response = file_get_contents(as you have it);
$googleobj = json_decode($response, TRUE);
$verified = $googleobj['success'];
if($verified === true) {
...
json_decode
中的参数true
使其成为关联数组。所以你可以像普通数组一样访问它。
查看 this 了解更多信息