![](/img/trans.png)
[英]FatalThrowableError - Call to a member 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;
}
}
附加信息:
{
"data":{
"username": "username_string",
"password": "password_string"
}
}
composer dump-autoload
并且问题仍然存在我找到了问题和解决方案。
因此,我正在使用Log
对异常进行一些记录,发现我的$uri
无效,因为在.env
文件中找不到我的 API BASE URL。 所以解决方案是做php artisan cache:clear
和 guzzle 作品。
对于阅读此问题的任何人,如果堆栈跟踪没有帮助您,请记住尝试Log
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.