Raja
Raja

Reputation: 792

Google reCaptcha 2.0 not working, Is it for curl?

after several trying (Explicitly render), I finally show the reCaptcha in my website.

Inside <head></head> tag..

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

and inside my form

<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>

and my php code is

<?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.";
    }
}
?>

and my getCurlData.php code is like

<?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;
}
?>

I have given you the entire details which I've tried. But the problem is, reCaptcha not succeeded. Means, it is showing the tick mark but the $msg showing "Please re-enter your reCAPTCHA 1." I am unable to find the error.

For your information, I am testing it in localhost, and if succeeded, I will upload it in my GoDaddy hosting site.

I have found this procedure, but not worked.

Update:

As per Stretch, I tried

$res=file_get_contents($url);

instead of

getCurlData($url);

And the problem solved. However I am putting my question open.

So is there no need of curl ? Why should we use curl in reCaptcha ?

Upvotes: 4

Views: 8551

Answers (1)

basiphobe
basiphobe

Reputation: 589

The reason it's not working is because you're trying to access HTTPS. The solution you shouldn't use is this:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

The solution you should use is this:

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

https://stackoverflow.com/a/316732/364841 has a link to the latest cacert.pem.

Upvotes: 2

Related Questions