file_get_contents()返回false for Google recaptcha v3

发布于 2025-01-28 11:03:24 字数 1444 浏览 2 评论 0原文

我一直在尝试通过使用以下PHP源代码来实现Google Recaptcha V3:

<script src="https://www.google.com/recaptcha/api.js?render=6Ldl-98fAAAAAKRPodblUlTcVgvrfWZ_8lODjmZA"></script>
<?php
    if(isset($_POST) && isset($_POST["btnSubmit"]))
    {
        $secretKey  = '6Ldl-98fAAAAAD3ekajHHVBi2X4fZTW37bI5IGUN';
        $token      = $_POST["g-token"];
        $ip         = $_SERVER['REMOTE_ADDR'];

        $url = "https://www.google.com/recaptcha/api/siteverify";
        $data = array('secret' => $secretKey, 'response' => $token, 'remoteip'=> $ip);

        // use key 'http' even if you send the request to https://...
        $options = array('http' => array(
            'method'  => 'POST',
            'content' => http_build_query($data)
        ));
        $context  = stream_context_create($options);
        $result = file_get_contents($url, false, $context);

        $response = json_decode($result);
        if($response->success)
        {
            echo '<center><h1>Validation Success!</h1></center>';
        }
        else
        {
            echo '<center><h1>Captcha Validation Failed..!</h1></center>';
        }
    }
?>

问题是,每次我运行此操作并逐行逐步浏览代码时,我都会注意到File_get_contents()函数始终返回false,并且正在分配。虚假的“ $ result”变量。为什么这是?

$result = file_get_contents($url, false, $context);

感谢我能得到的任何建议。

I have been trying to implement Google reCAPTCHA v3 by using the following PHP source code:

<script src="https://www.google.com/recaptcha/api.js?render=6Ldl-98fAAAAAKRPodblUlTcVgvrfWZ_8lODjmZA"></script>
<?php
    if(isset($_POST) && isset($_POST["btnSubmit"]))
    {
        $secretKey  = '6Ldl-98fAAAAAD3ekajHHVBi2X4fZTW37bI5IGUN';
        $token      = $_POST["g-token"];
        $ip         = $_SERVER['REMOTE_ADDR'];

        $url = "https://www.google.com/recaptcha/api/siteverify";
        $data = array('secret' => $secretKey, 'response' => $token, 'remoteip'=> $ip);

        // use key 'http' even if you send the request to https://...
        $options = array('http' => array(
            'method'  => 'POST',
            'content' => http_build_query($data)
        ));
        $context  = stream_context_create($options);
        $result = file_get_contents($url, false, $context);

        $response = json_decode($result);
        if($response->success)
        {
            echo '<center><h1>Validation Success!</h1></center>';
        }
        else
        {
            echo '<center><h1>Captcha Validation Failed..!</h1></center>';
        }
    }
?>

The problem is that every time I run this and step through the code line by line, I notice that the file_get_contents() function is always returning false and is assigning false to the '$result' variable. Why is this?

$result = file_get_contents($url, false, $context);

I would appreciate any advice that I can get.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2025-02-04 11:03:24

从文档中:“如果无法解码JSON或编码数据比嵌套限制更深,则返回null。”

显示US $响应,因此var_dump($ wendesp)。在recaptcha v2中,我最近有空字符串或象征性。这就是为什么我要求响应的原因,JSON DECODE将解码是否有适当的JSON值。

我敢打赌,您将获得空的结果,JSON_DECODE失败。

From docs: 'null is returned if the json cannot be decoded or if the encoded data is deeper than the nesting limit.'

Show us $response, so var_dump($response). In recaptcha v2 i had empty string or token lately. That's why I ask for response, json decode will decode if there is proper json value.

I'd bet you get empty result and json_decode fails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文