[英]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:附加信息:
{
"data":{
"username": "username_string",
"password": "password_string"
}
}
composer dump-autoload
and the problem still persistscomposer 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.