【问题标题】:Google reCaptcha 2.0 not working, Is it for curl?Google reCaptcha 2.0 无法正常工作,是用于 curl 吗?
【发布时间】:2015-07-06 12:00:18
【问题描述】:

经过几次尝试(显式渲染),我终于在我的网站上显示了reCaptcha。

在<head></head>标签内..

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

在我的表单中

<form action="" method="post">
    <input placeholder="Enter Your Name" type="text" required/>
    <input placeholder="Enter Your Email" type="email" required/>
    <input placeholder="Enter Your Password" type="password" required/>
    <div class="g-recaptcha" data-sitekey="My_reCaptcha_Site_Key"></div>
    <?php echo $msg; ?>
    <input type="submit" value="Register">
</form>

而我的php 代码是

<?php
$msg='';
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $recaptcha=$_POST['g-recaptcha-response'];
    if(!empty($recaptcha)){
        include 'getCurlData.php';
        $google_url="https://www.google.com/recaptcha/api/siteverify";
        $secret='My_reCaptcha_Secret_Key';
        $ip=$_SERVER['REMOTE_ADDR'];
        $url=$google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
        $res=getCurlData($url);
        $res= json_decode($res, true);
        //reCaptcha success check 
        if($res['success']){
            $msg="Your reCAPTCHA succeeded.";
        } else {
            $msg="Please re-enter your reCAPTCHA 1.";
        }
    } else {
        $msg="Please re-enter your reCAPTCHA 2.";
    }
}
?>

而我的getCurlData.php 代码就像

<?php
function getCurlData($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 120);
$curlData = curl_exec($curl);
curl_close($curl);
return $curlData;
}
?>

我已经向您提供了我尝试过的全部细节。但问题是,reCaptcha 没有成功。意思是,它显示了勾号,但 $msg 显示“请重新输入您的 reCAPTCHA 1”。我找不到错误。

供您参考,我正在localhost 进行测试,如果成功,我会将其上传到我的GoDaddy 托管站点。

我找到了this procedure,但没有找到。

更新:

根据 Stretch,我尝试过

$res=file_get_contents($url);

而不是

getCurlData($url);

问题解决了。但是,我提出了我的问题。

那么就不需要curl了吗?我们为什么要在reCaptcha中使用curl?

【问题讨论】:

  • 代替getCurlData() 试试file_get_contents() 看看会发生什么。
  • 感谢@Stretch,它成功了。你可以看到我的更新。
  • file_get_contents() 仅当您的 php ini 文件中的 allow_url_fopen 设置为 1 时才有效。通常您无法更改实时服务器上的 php ini 文件,因为您使用的是共享主机。在这种情况下,您可以依赖 Curl。

标签: php recaptcha


【解决方案1】:

它不工作的原因是因为您正在尝试访问 HTTPS。您不应该使用的解决方案是:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

您应该使用的解决方案是:

curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem');

https://stackoverflow.com/a/316732/364841 有一个指向最新 cacert.pem 的链接。

【讨论】:

    猜你喜欢
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2013-05-13
    • 2011-12-10
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多