How to log Guzzle Http request response in file using Laravel 7+
Asked Answered
C

0

0

I am using

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

I am consuming some third party API's in laravel using Http Client

What I need: I need to log every request and response in the file to track for further use.

I was follow some of blogs and answers.

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

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

[3] Laravel - Log Guzzle requests to file

Above [3] link working as mentioned when calling in web.php with given method. This worked

$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());

But I am using this single line API call using HTTP class like this where I am not creating any new object by own.

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

Can someone help to achieve this, how my API Call logs into a file?

Curly answered 10/6, 2021 at 11:16 Comment(2)
what is the problem with the [3] stackoverflow answer you can use any of the two second one being quick.Ordinate
@Ordinate Second one is not supported by PHP 8, and I am using laravel.com/docs/8.x/http-client where shorthand way to call API mentioned above where internally client was created we just use HTTP facade, which means we do not create any client on our own, Http facade internally creates in the vendor/laravel/framework/src/Illuminate/Http/Client buildClient() method .Curly

© 2022 - 2024 — McMap. All rights reserved.