简体   繁体   English

PHP 和 Google 的 reCaptcha v2 - 每次都是空响应

[英]PHP and Google's reCaptcha v2 - empty response every time

We have a contact.html form that uses reCaptcha v2, whose backend processing is in a php file.我们有一个使用 reCaptcha v2 的 contact.html 表单,其后端处理在 php 文件中。

I've taken enough steps to understand that when we send the verification to google's api, the response comes back empty.我已经采取了足够的步骤来了解当我们将验证发送到谷歌的 api 时,响应返回为空。 Below is code that gave me this proof.下面是给我这个证明的代码。

$url = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST["g-recaptcha-response"].'&remoteip='.$_SERVER['REMOTE_ADDR'];
    $verify = file_get_contents($url);
    echo $url;
    if (empty($verify)) echo 'Failed to fetch data';`

However, when I manually enter the url into a browser, I get a JSON response back that indicates success.但是,当我在浏览器中手动输入 url 时,我收到 JSON 响应,表明成功。

What, then, is the difference?那么,有什么区别呢? Why would file_get_contents return empty if a simple get request from a Chrome browser give me trouble?如果来自 Chrome 浏览器的简单 get 请求给我带来麻烦,为什么 file_get_contents 会返回空?

I have read that file_get_contents is synchronous, so I wouldn't expect this is just a noob error on waiting for the response.我已经读到 file_get_contents 是同步的,所以我不希望这只是等待响应时出现的菜鸟错误。

Any help would be appreciated, this is my very first time working with PHP.任何帮助将不胜感激,这是我第一次使用 PHP。 It's not hard, but I may be missing something vital.这并不难,但我可能错过了一些重要的东西。

Sorry everyone, I can't understand why, but the problem was in the method used to access the site verify. 抱歉,我不明白为什么,但是问题出在用于访问站点验证的方法中。

Using curl syntax, I finally got it working. 使用curl语法,我终于可以使用它了。

Change the configuration in php.ini file and don't need curl.更改 php.ini 文件中的配置,不需要 curl。

allow_url_fopen= 0 to allow_url_fopen= 1 allow_url_fopen= 0到 allow_url_fopen= 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM