簡體   English   中英

當 curl 和 Postman 完全正常時,Guzzle 錯誤“調用成員 function getStatusCode() on null”

[英]Guzzle error “Call to a member function getStatusCode() on null” when curl and Postman are perfectly fine

我在我的項目中將 Guzzle 設置為我的 Laravel 6 的 HTTP 客戶端。 它在每個 API 命中時都能完美運行並給出響應。 但突然它返回“調用成員 function getStatusCode() on null”錯誤。

我嘗試使用 Postman 和 curl 來擊中 API,當 Guzzle 沒有時,它們會返回正確的響應。

RequestException 響應的 print_r 不是返回響應,而是返回 null。

I've tried to debug the REST API to test where the request go when hitting it using Guzzle, and the request didn't even hit to the destination function .

這是我的 Guzzle 代碼:

class ApiCallback
{
    public static function request($method, $uri, $params = [], $useAccessToken = true){
        $client = new \GuzzleHttp\Client();
        $response = null;

        $jsonObj = null;
        try{
            if($useAccessToken){
                $response = $client->request(
                    strtoupper($method),
                    $uri,
                    [
                        "headers" =>[
                            'Accept' => '*/*',
                            "Authorization" => "Bearer ".Session::get('access_token'),
                            'Content-Type' => 'application/json',
                            'User-Agent' => 'saranaku-'.Session::get('id'),
                            'Content-Length' => '0',
                            'Accept-Encoding' => 'gzip, deflate, br',
                            'Connection' => 'keep-alive'
                        ]
                        ,
                        "body" => \json_encode(
                            $params
                        )
                    ]
                );
            }else{
                $response = $client->request(
                    strtoupper($method),
                    $uri,
                    [
                        "headers" =>[
                            'Accept' => '*/*',
                            'Content-Type' => 'application/json',
                            'User-Agent' => "saranaku-guest",
                            'Content-Length' => '0',
                            'Accept-Encoding' => 'gzip, deflate, br',
                            'Connection' => 'keep-alive'
                        ]
                        ,
                        "body" => \json_encode(
                            $params
                        )
                    ]
                );
            }
            $jsonObj = json_decode((string) $response->getBody());
            return new BaseResponse(
                $jsonObj->status ?? false,
                $jsonObj->code ?? null,
                $jsonObj->message ?? null ,
                $jsonObj->data ?? null
            );
        }catch(RequestException $e){
//            echo Psr7\str($e->getRequest());
//            echo Psr7\str($e->getResponse());
            $jsonObj = $e->getResponse();
            print_r($e->getResponse());
        }
        return new BaseResponse(
            $jsonObj->status ?? false,
            $jsonObj->getStatusCode() ?? null,
            $jsonObj->getReasonPhrase() ?? null ,
            $jsonObj->data ?? null
        );
    }

    public static function requestWithoutAccessToken($method, $url, $params = []){
        return ApiCallback::request($method, $url, $params, false);
    }
}

final class BaseResponse
{
    public $status;
    public $code;
    public $message;
    public $data;

    public function __construct($status,$code,$message = "",$data = [])
    {
        $this->status = $status;
        $this->code = $code;
        $this->message = $message;
        $this->data = $data;
    }
}

附加信息:

  1. REST API 在 localhost 中運行,端點為:/v1/login
  2. HTTP 方法是 POST
  3. REST API 正在使用 Java Z38008DD81C2F4D798Z Boot v16E0CE8AF1D1。
  4. 請求正文是
{
     "data":{
          "username": "username_string",
          "password": "password_string"
     }
}
  1. 我已經嘗試過這些清除緩存,並執行composer dump-autoload並且問題仍然存在

我找到了問題和解決方案。

因此,我正在使用Log對異常進行一些記錄,發現我的$uri無效,因為在.env文件中找不到我的 API BASE URL。 所以解決方案是做php artisan cache:clear和 guzzle 作品。

對於閱讀此問題的任何人,如果堆棧跟蹤沒有幫助您,請記住嘗試Log

暫無
暫無

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

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