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

I'm setting up Guzzle as my Laravel 6's HTTP Client in my project.我在我的项目中将 Guzzle 设置为我的 Laravel 6 的 HTTP 客户端。 It was perfectly working and giving responses on every API hit.它在每个 API 命中时都能完美运行并给出响应。 But suddenly it returns "Call to a member function getStatusCode() on null" error.但突然它返回“调用成员 function getStatusCode() on null”错误。

I've tried using Postman and curl to hit the API, and they return the right response when Guzzle not.我尝试使用 Postman 和 curl 来击中 API,当 Guzzle 没有时,它们会返回正确的响应。

Instead of returning response, the print_r for RequestException response's return null. 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 . 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 .

Here's my Guzzle code:这是我的 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;
    }
}

Additional information:附加信息:

  1. The REST API is running in localhost with endpoint: /v1/login REST API 在 localhost 中运行,端点为:/v1/login
  2. The HTTP method is POST HTTP 方法是 POST
  3. The REST API is using Java Spring Boot v1.5.6 REST API 正在使用 Java Z38008DD81C2F4D798Z Boot v16E0CE8AF1D1。
  4. The Request Body is请求正文是
{
     "data":{
          "username": "username_string",
          "password": "password_string"
     }
}
  1. I've tried these clearing caches, and doing composer dump-autoload and the problem still persists我已经尝试过这些清除缓存,并执行composer dump-autoload并且问题仍然存在

I found the problem and the solution.我找到了问题和解决方案。

So I'm doing some Logging on the exception with Log and found out that my $uri is not valid due my API BASE URL was not found on .env file.因此,我正在使用Log对异常进行一些记录,发现我的$uri无效,因为在.env文件中找不到我的 API BASE URL。 So the solution is doing php artisan cache:clear and the guzzle works.所以解决方案是做php artisan cache:clear和 guzzle 作品。

For anyone who reading this question, please remember to try Log if the stack trace didn't help you out.对于阅读此问题的任何人,如果堆栈跟踪没有帮助您,请记住尝试Log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM