簡體   English   中英

Guzzle:調用未定義的方法 GuzzleHttp\\\\Psr7\\\\Stream::getStatusCode()

[英]Guzzle: Call to undefined method GuzzleHttp\\\\Psr7\\\\Stream::getStatusCode()

我正在 mu laravel 應用程序中試用 guzzle 以使用 FCM 通知,出於某種原因,當我嘗試獲取響應狀態代碼時出現以下錯誤,我是否也在使用 guzzle 正確的語法? 他們似乎已經更新了他們的。

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

我的方法:

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;
    }

提前致謝

您正在覆蓋響應,然后嘗試從 stream 獲取狀態代碼。您應該改為

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

請注意,我將 getStatus 方法移到了 getBody 之上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM