谷歌reCaptcha 2.0无法正常工作,是否卷曲?

时间:2015-04-27 20:57:24

标签: php recaptcha

经过多次尝试(明确渲染)后,我终于在我的网站上显示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我们为什么要在curl中使用reCaptcha

1 个答案:

答案 0 :(得分:1)

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

xsl:key

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

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

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