Google Recaptcha v3 example demo

后端 未结 7 886
故里飘歌
故里飘歌 2020-12-02 10:44

Until now, I was working with Google Recaptcha v2, but now I want to update my WebApp using the lastest version (v3).

Is it possible to anyone add a fully working Go

相关标签:
7条回答
  • 2020-12-02 11:17

    I process POST on PHP from an angular ajax call. I also like to see the SCORE from google.

    This works well for me...

    $postData = json_decode(file_get_contents('php://input'), true); //get data sent via post
    $captcha = $postData['g-recaptcha-response'];
    
    header('Content-Type: application/json');
    if($captcha === ''){
        //Do something with error
        echo '{ "status" : "bad", "score" : "none"}';
    } else {
        $secret   = 'your-secret-key';
        $response = file_get_contents(
            "https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']
        );
        // use json_decode to extract json response
        $response = json_decode($response);
    
        if ($response->success === false) {
            //Do something with error
            echo '{ "status" : "bad", "score" : "none"}';
        }else if ($response->success==true && $response->score <= 0.5) {
            echo '{ "status" : "bad", "score" : "'.$response->score.'"}';
        }else {
            echo '{ "status" : "ok", "score" : "'.$response->score.'"}';
        }
    }
    

    On HTML

    <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response">
    

    On js

    $scope.grabCaptchaV3=function(){
        var params = {
                         method: 'POST',
                         url: 'api/recaptcha.php',
                         headers: {
                           'Content-Type': undefined
                         },
                         data:   {'g-recaptcha-response' : myCaptcha }
         }
         $http(params).then(function(result){ 
                    console.log(result.data);
         }, function(response){
                    console.log(response.statusText);
         }); 
    }
    
    0 讨论(0)
提交回复
热议问题