首页 > 解决方案 > 如何使用 Laravel 7+ 在文件中记录 Guzzle Http 请求响应

问题描述

我在用

我正在使用Http Client在 laravel 中使用一些第三方 API

我需要什么:我需要在文件中记录每个请求和响应以跟踪以供进一步使用。

我正在关注一些博客和答案。

1 https://medium.com/@sirajul.anik/logging-request-response-for-guzzle-bbea1f54cd87

[2] https://github.com/bilfeldt/laravel-http-client-logger

[3] Laravel - 记录 Guzzle 请求文件

使用给定方法调用 web.php 时,上述 [3] 链接的工作方式如前所述。 这有效

$client = app('GuzzleClient')([
    'base_uri'  => 'https://jsonplaceholder.typicode.com/',
    'timeout'   => 10,
    'verify'    => false,
]);

$request = $client->get('users', [
    'headers' => [ 'accept' =>  'application/json']
]);

$response = json_decode((string) $request->getBody());

但是我正在使用这样的 HTTP 类来使用这个单行 API 调用,我不会自己创建任何新对象。

$response = Http::withBody(
    json_encode($reqArray), 'application/json')->withToken($token)->post($SearchByNameApi);

有人可以帮助实现这一点,我的 API 调用如何登录到文件中?

标签: laravellogginglaravel-7guzzle

解决方案


推荐阅读