繁体   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