https://github.com/jhoffland/guzzle-formatter
PHP Guzzle request/response to HTTP message formatter
https://github.com/jhoffland/guzzle-formatter
guzzle http php
Last synced: 4 months ago
JSON representation
PHP Guzzle request/response to HTTP message formatter
- Host: GitHub
- URL: https://github.com/jhoffland/guzzle-formatter
- Owner: jhoffland
- License: mit
- Created: 2022-03-17T08:58:49.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-10-20T17:05:29.000Z (over 3 years ago)
- Last Synced: 2025-10-07T11:19:39.393Z (6 months ago)
- Topics: guzzle, http, php
- Language: PHP
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Guzzle Formatter
[](https://github.com/jhoffland/guzzle-formatter/actions/workflows/testing.yml)
[](https://github.styleci.io/repos/470917304?branch=main)
PHP library for formatting Guzzle requests and responses to [HTTP messages](https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages).
## Installation
[](https://packagist.org/packages/jhoffland/guzzle-formatter)
[](https://packagist.org/packages/jhoffland/guzzle-formatter)
Install this library directly with Composer:
```shell
composer require jhoffland/guzzle-formatter
```
Add `--dev` if this library is not needed in a production environment.
## Usage
```php
use GuzzleFormatter\RequestFormatter;
use GuzzleFormatter\ResponseFormatter;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$request = new Request('GET', 'https://github.com/jhoffland/guzzle-formatter');
echo (new RequestFormatter())->http($request); // Results in the formatted HTTP request message.
$response = (new Client())->send($request);
echo (new ResponseFormatter())->http($response); // Results in the formatted HTTP response message.
```
### Using middleware
Logging all requests performed and/or responses received by a Guzzle client, using [middleware](https://docs.guzzlephp.org/en/stable/handlers-and-middleware.html#middleware).
The HTTP request and/or response messages are written to the file, specified when creating the `HttpFormatterMiddleware` class instance.
```php
use GuzzleFormatter\Middleware\HttpFormatterMiddleware;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
$formatterMiddleware = new HttpFormatterMiddleware('/path/to/output-file.txt');
$handlerStack = HandlerStack::create();
$handlerStack->after('prepare_body', $formatterMiddleware->requests(), 'http_request_formatter');
$handlerStack->after('prepare_body', $formatterMiddleware->responses(), 'http_response_formatter');
$client = new Client([
'handler' => $handlerStack,
]);
$client->get('https://github.com/jhoffland/guzzle-formatter');
```
### Available options
#### 1. End of Line character
Available when creating an instance of [`RequestFormatter`](src/RequestFormatter.php) & [`ResponseFormatter`](src/ResponseFormatter.php).
#### 2. Hide sensitive headers
Available for when formatting an HTTP message with [`RequestFormatter`](src/RequestFormatter.php) & [`ResponseFormatter`](src/ResponseFormatter.php) & when creating an instance of [`HttpFormatterMiddleware`](src/Middleware/HttpFormatterMiddleware.php).
The headers seen as sensitive can be found in the array [`Formatter::SENSITIVE_HEADERS`](src/Formatter.php).
## Supported PHP & package versions
[](composer.json)
[](composer.json)
This library is tested with PHP 7.4, 8.0 and 8.1.
Check the [`composer.json`](composer.json) file for the supported package versions.
Feel free to add support for additional versions to this library.
## Contributing
Feel free to contribute to this library. Contribute by forking the [GitHub repository](https://github.com/jhoffland/guzzle-formatter) and opening a pull request.
When opening a pull request, please make sure that:
- The pull request has a clear title;
- The pull request does not consist of too many (unnecessary/small) commits;
- The [StyleCI](https://github.styleci.io/repos/470917304) analysis pass;
- The PHPUnit tests pass.
## ToDo's
- [ ] Add test for formatting request when making an request to an URL without path (e.g. to https://google.com instead of to https://google.com/).
- [ ] Add test for hiding and not-hiding sensitive headers when using the `HttpFormatterMiddleware`.
- [ ] Add cURL formatter.