'Guzzle: Call to undefined method GuzzleHttp\\\\Psr7\\\\Stream::getStatusCode()

I'm trying out guzzle in mu laravel app in order to use FCM notifications, for some reason I'm getting the following error when I try to get response status code, also am I using guzzle correct syntax? they seem to have updated theirs.

Call to undefined method GuzzleHttp\\\\Psr7\\\\Stream::getStatusCode()

My method:

public function send($user,$title,$body, $data = false , $type, $image='')
    { 
        $client = new Client();
        
        $url = 'https://fcm.googleapis.com/fcm/send';
        $serverKey = config('services.firebase.api_key');

        $headers = 
        [
            'Content-Type' => 'application/json',
            'Authorization' => 'key='.$serverKey,
        ];

        $fields = 
        [
            'registration_ids' => [ $user['fcm_token'] ],
            'to' => $user['fcm_token'],
            "notification" => 
            [
                "title" => $title,
                "body" => $body,
                "sound" => "default",
            ],
            "priority" => 10,
            'data' => $data,
            "android" => [ "priority" => "high" ]    
        ];

        $fields = json_encode ( $fields );

        try 
        {
            $response = $client->request('POST',$url,[
                'headers' => $headers,
                "body" => $fields,
            ]);

            $response = $response->getBody();
            $statusCode = $response->getStatusCode();
            
        }
        catch (ClientException $e) 
        {
            
            $response = $e->getResponse();
            $response = $response->getBody()->getContents();
            $statusCode = $response->getStatusCode();
            
        }

        $result =
        [
            'response' => $response,
            'statusCode' => $statusCode
        ];

        return $result;
    }

Thanks in advance



Solution 1:[1]

You are overwriting the response and then trying to get the status code from the stream. You should instead do

$response = $e->getResponse();
$statusCode = $response->getStatusCode();
$response = $response->getBody()->getContents();

Notice I moved the getStatus method above the getBody.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bernard Wiesner