https://github.com/dlzer/http-utils
Common HTTP Utilities for PHP Request and Response in PSR-7
https://github.com/dlzer/http-utils
Last synced: 10 months ago
JSON representation
Common HTTP Utilities for PHP Request and Response in PSR-7
- Host: GitHub
- URL: https://github.com/dlzer/http-utils
- Owner: DLzer
- License: mit
- Created: 2021-10-01T19:16:06.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-10-01T19:36:17.000Z (over 4 years ago)
- Last Synced: 2024-04-29T10:22:44.353Z (about 2 years ago)
- Language: PHP
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PSR Http Message Utilities


Utility classes and constants to provide quick reference usage for request methods and responses.
## Installation
```bash
compose require dlzer/http-utils
```
## Usage
When used in conjunction with [PHP-FIG](https://github.com/php-fig/http-message-util) Status Code Interface.
```php
// Custom responder method
public function withJson(
ResponseInterface $response,
$statusCode,
$message,
$data = null,
int $options = 0
): ResponseInterface
{
return $response->write(json_encode([
"status" => $statusCode,
"message" => $message,
"data" => $data
], $options);
);
}
// Usage
$data = ["connection" => true];
return $this->responder->withJson(
$response, // The response interface
StatusCodeInterface::STATUS_OK // The status code interface
StatusCodeMessage::STATUS_OK // The status message interface: "OK"
$data // The response data
);
```
Output:
```json
{
"status": 200,
"message": "OK",
"data": {
"connection": true
}
}
```