How can the example code provided in the documentation be adapted to work correctly in the given scenario?

The issue with the example code provided in the documentation is that it does not properly handle the scenario where the API response is not successful. To solve this issue, we need to check if the API response is successful before attempting to access the data. This can be done by verifying the HTTP status code of the response.

// Example code adapted to handle unsuccessful API response scenario
$response = $client->request('GET', 'https://api.example.com/data');
if ($response->getStatusCode() == 200) {
    $data = json_decode($response->getBody(), true);
    // Process the data here
} else {
    echo 'Error: API request was not successful';
}