How to Get Response Body from Guzzle in Laravel?

I’m using GuzzleHttp for making HTTP requests on my Laravel project. However, I’m having trouble retrieving the body of the response using Guzzle. Here’s a simplified version of my code:

$client = new \GuzzleHttp\Client();
$response = $client->request('GET', $api_url);
$body = $response->getBody();

When I try to access the body of the response using $response->getBody(), it does not give me the expected result. Can anyone guide me on how to properly get the body of a response from Guzzle in Laravel?

Guzzle uses PSR-7, which stores the response body in a stream by default. To get the body content, you can use either of these methods:

$contents = (string) $response->getBody();

OR

$contents = $response->getBody()->getContents();

Both of these methods will retrieve the content of the response body as a string, allowing you to access the data easily.