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

rameezisrar's avatar

Guzzle response success on Captcha

$client = new Client();
        $response = $client->request('POST', 'https://www.google.com/recaptcha/api/siteverify',[

            'secret' => config('services.recaptcha.secret'),
            'response' => $request->input('g-recaptcha-response'),
            'remoteip' => $_SERVER['REMOTE_ADDR']

        ]);

     return $response->getBody();

the response is

{ "success": false, "error-codes": [ "missing-input-response", "missing-input-secret" ] }

But when i try

if( $response->getBody['success']) 

i get this error

Cannot use object of type GuzzleHttp\Psr7\Stream as array

i want to get the success field out of it.

0 likes
2 replies
grenadecx's avatar
Level 7

If I remember correctly, getBody returns the json string itself. Well more specifically it returns the exact response back. But in your case that's a json string. So you need to decode that:

$body = json_decode($response->getBody(),true);

now $body should be an array, so you should be able to access status from it, you can run dd on it to see the results. It's possible you need to run ->getContents() on the getBody response as well.

1 like
rameezisrar's avatar

@grenadecx Yes it does return the JSON response but I wasn't converting it into an associative array which was resulting in the error I mentioned :) Thanks a bunch :)

Please or to participate in this conversation.