如何使用 Laravel 7+ 在文件中记录 Guzzle Http 请求响应

2024-04-05

我在用

  • Laravel 框架 7.28.4
  • guzzlehttp/guzzle: "^7.2",
  • PHP 8.0.5

我正在 laravel 中使用一些第三方 APIHttp客户端 https://laravel.com/docs/7.x/http-client

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

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

1 https://laravel.com/docs/7.x/http-client https://medium.com/@sirajul.anik/logging-request-response-for-guzzle-bbea1f54cd87 https://medium.com/@sirajul.anik/logging-request-response-for-guzzle-bbea1f54cd87

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

[3] Laravel - 将 Guzzle 请求记录到文件 https://stackoverflow.com/questions/57534590/laravel-log-guzzle-requests-to-file

使用给定方法调用 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 调用如何记录到文件中?


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Laravel 7+ 在文件中记录 Guzzle Http 请求响应 的相关文章

随机推荐