Search code examples
phplaravelrecaptcha

validation recaptchaV3 in Laravel


I´m traying to add recaptchaV3 into one web, but always returned me error in json_decode() i cheked my .env variable values with echo and it´s ok, but response from validation it´s wrong. In developped google console, i have my ky configurated. I´m traying with this code:

$url = "https://www.google.com/recaptcha/api/siteverify";
        $data = [
            'secret'   => env("RECAPTCHAV3_SECRET"),
            'response' => request('recaptcha')
        ];

        $options = [
            'http'  => [
                'header'    => "Content-type: application/x-www-form-urlencoded\r\n",
                'method'    => 'POST',
                'content'   => http_build_query($data)
            ]
        ];
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        $resultJson = json_decode($result);

        if($resultJson->success != true){
            echo "Captcha Error"; 
            exit();

        }else{

            $data = $request->all();

            // SEND EMAIL
            \Mail::send('web.emailContact', $data, function($message) use ($request)
            {
                $message->from($request->email, $request->name);
                $message->subject("Prueba formulario contacto");
                $message->to("daviserraalonso@gmail.com");
            });
        }

i have to say that captcha V3 image it´s show in right corner from web... for this i think that i integrated captcha well, but when i send my form, returned Catcha Error

fristly i have this code:

$validate = \Validator::make(Input::all(), [
            'g-recaptcha-response' => 'required|recaptchav3:register,0.5',
            'name'  => 'required|min:3',
            'email' => 'required|email',
            'phone' => 'required|min:9'
        ]);

        //check if validation it´s correct
        if($validate->fails()){
            \Redirect::back()->withErrors($validate->messages())->withInput();
        }else{
            return \Redirect::route('contact')->withMessage("Mensaje enviado correctamente");
        }


        $score = \RecaptchaV3::verify($request->get('g-recaptcha-response'));
        if($score > 0.7) {
            // go
        } elseif($score > 0.3) {
            // require additional email verification
        } else {
            return abort(400, 'You are most likely a bot');
        }

but this code, always returnd also else block.

i hope that aanybody can help me please.

Rewards


Solution

  • i resolve my problem:

    my captcha name was wrong

    $url = "https://www.google.com/recaptcha/api/siteverify";
            $data = [
                'secret'   => env("RECAPTCHAV3_SECRET"),
                'response' => request('reCaptcha')
            ];
    
            $options = [
                'http'  => [
                    'header'    => "Content-type: application/x-www-form-urlencoded\r\n",
                    'method'    => 'POST',
                    'content'   => http_build_query($data)
                ]
            ];
            
            $context = stream_context_create($options);
            $result = file_get_contents($url, false, $context);
            $resultJson = json_decode($result);
    
            if($resultJson->success != true){
                echo "Captcha Error"; 
                exit();