Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

Integrating Google reCAPTCHA v3 inside a login form

Hello everyone!

I'm trying to integrate Google reCAPTCHA v3 in my administrator login form. Here's what I got so far:

            $request->validate([
                'email' => 'required',
                'password' => 'required',
                'googlerecaptcha' => 'required'
            ], [
                'googlerecaptcha.required' => 'You must pass reCAPTCHA!'
            ]);

            $captcha = real_escape_string($request->googlerecaptcha);

            $request_url = 'https://www.google.com/recaptcha/api/siteverify';

            $request_data = [
                'secret' => 'reCAPTCHA_secret_key',
                'response' => $captcha
            ];

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $request_url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $request_data);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            $response_body = curl_exec($ch);

            curl_close ($ch);

            $response_data = json_decode($response_body, true);

            if ($response_data['success'] == false) {

                $credentials = $request->only(['email', 'password']);

                if(!auth()->validate($credentials)) {
                    return redirect()->route('admin.login')->withErrors(trans('auth.failed'))->withInput();
                }

                $user = User::where('email', $request->email)->first();
        
                auth()->login($user, $request->remember);

                return $this->authenticated($request, $user);
            } else {
                return back()->with('error', 'reCAPTCHA failed! Please, try again.')->withInput();
            }

I'm curious if there is a way to convert these curl operations to Laravel Guzzle Http operations? I want to optimize this code somehow. Do you have any ideas?

0 likes
2 replies
lat4732's avatar
Level 12

@Sergiu17 I get error for missing parameters when using Http::post($request_url, $request_data). I have no idea why.

Please or to participate in this conversation.