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

alexhackney's avatar

Google ReCaptcha + Laravel + GuzzleHttp

I'm having issues getting this working.

I realize there are packages that will do this for me, but how do I learn if I don't try to do it on my own?

So I've got everything working up until I need to make the API post to google to see if the recaptcha is valid.

$recaptcha = Input::get('g-recaptcha-response');
        
        $client = new Client([
            'base_uri' => 'https://google.com/recaptcha/api/'
            ]);

        $response = $client->request('POST', 'siteverify', [
            'query' => [
            'secret' => env('GOOGLE_RECAPTCHA_SECRET'),
            'response' => $recaptcha]]);

        dd($response);

But all I get when I run that is this.

Response {#223 ▼
  -reasonPhrase: "OK"
  -statusCode: 200
  -headers: array:12 [▶]
  -headerLines: array:12 [▶]
  -protocol: "1.1"
  -stream: Stream {#221 ▶}
}

I'm not sure what I'm doing wrong. I am going to take that response if its true and then allow the message to be sent and send back if not.

0 likes
4 replies
alexhackney's avatar

So I have found out that I have to getBody()->getContents(); of the response. Now I just have to parse that.

$recaptcha = Input::get('g-recaptcha-response');
        
        $client = new Client([
            'base_uri' => 'https://google.com/recaptcha/api/',
            'timeout' => 2.0
            ]);

        $response = $client->request('POST', 'siteverify', [
            'query' => [
            'secret' => env('GOOGLE_RECAPTCHA_SECRET'),
            'response' => $recaptcha]]);

        dd($response->getBody()->getContents());
alexhackney's avatar

And now I've got a working solution.

    $recaptcha = Input::get('g-recaptcha-response');
        
        $client = new Client([
            'base_uri' => 'https://google.com/recaptcha/api/',
            'timeout' => 2.0
            ]);

        $response = $client->request('POST', 'siteverify', [
            'query' => [
            'secret' => env('GOOGLE_RECAPTCHA_SECRET'),
            'response' => $recaptcha]]);

        dd(json_decode($response->getBody()));

This returns the json from google to give me a success => true or false which I can proceed with accordingly.

3 likes
Robstar's avatar

In case any is reading this in 2021, now Laravel has it;s own wrapper around Guzzle, this can be acheived as follows:

use Illuminate\Support\Facades\Http;
    
    $response = Http::asForm()
      ->post('https://www.google.com/recaptcha/api/siteverify', [
        'secret' => 'your secret key',
        'response' => 'g-response from your form',
      ])->json();

$response is the array described at https://developers.google.com/recaptcha/docs/verify#api-response

4 likes

Please or to participate in this conversation.