Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bayfrontmedia/php-http-response
Easily send HTTP responses.
https://github.com/bayfrontmedia/php-http-response
header http method php redirect response url
Last synced: about 5 hours ago
JSON representation
Easily send HTTP responses.
- Host: GitHub
- URL: https://github.com/bayfrontmedia/php-http-response
- Owner: bayfrontmedia
- License: mit
- Created: 2020-08-10T17:31:15.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-02-03T17:51:40.000Z (almost 2 years ago)
- Last Synced: 2024-08-09T13:14:54.554Z (3 months ago)
- Topics: header, http, method, php, redirect, response, url
- Language: PHP
- Homepage:
- Size: 40 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
## PHP HTTP response
Easily send HTTP responses.
- [License](#license)
- [Author](#author)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)## License
This project is open source and available under the [MIT License](LICENSE).
## Author
- [Bayfront Media homepage](https://www.bayfrontmedia.com?utm_source=github&utm_medium=direct)
- [Bayfront Media GitHub](https://github.com/bayfrontmedia)## Requirements
- PHP `^8.0`
- JSON PHP extension## Installation
```
composer require bayfrontmedia/php-http-response
```## Usage
- [reset](#reset)
- [setStatusCode](#setstatuscode)
- [getStatusCode](#getstatuscode)
- [removeHeaders](#removeheaders)
- [setHeaders](#setheaders)
- [getHeaders](#getheaders)
- [setBody](#setbody)
- [getBody](#getbody)
- [send](#send)
- [sendJson](#sendjson)
- [redirect](#redirect)
### reset
**Description:**
Resets all headers (including status code) and body.
**Parameters:**
- None
**Returns:**
- (self)
**Example:**
```
$response->reset();
```
### setStatusCode
**Description:**
Sets status code to be sent with response.
**Parameters:**
- `$status` (int)
**Returns:**
- (self)
**Throws:**
- `Bayfront\HttpResponse\InvalidStatusCodeException`
**Example:**
```
use Bayfront\HttpResponse\InvalidStatusCodeException;
use Bayfront\HttpResponse\Response;$response = new Response();
try {
$response->setStatusCode(429);
} catch (InvalidStatusCodeException $e) {
die($e->getMessage());
}
```
### getStatusCode
**Description:**
Returns the status code and associated phrase to be sent with response.
**Parameters:**
- None
**Returns:**
- (array)
**Example:**
```
print_r($response->getStatusCode());
```
### removeHeaders
**Description:**
Sets header values to be removed with the response.
**Parameters:**
- `$headers` (array)
**Returns:**
- (self)
**Example:**
```
$response->removeHeaders([
'X-Powered-By'
]);
```
### setHeaders
**Description:**
Sets header values to be sent with the response.
**Parameters:**
- `$headers` (array)
**Returns:**
- (self)
**Example:**
```
$response->setHeaders([
'X-Rate-Limit-Limit' => 100,
'X-Rate-Limit-Remaining' => 99
]);
```
### getHeaders
**Description:**
Returns array of headers to be sent with the response.
**Parameters:**
- None
**Returns:**
- (array)
**Example:**
```
print_r($response->getHeaders());
```
### setBody
**Description:**
Sets body to be sent with the response.
**Parameters:**
- `$body` (string)
**Returns:**
- (self)
**Example:**
```
$response->setBody('This is the response body.');
```
### getBody
**Description:**
Returns body to be sent with the response.
**Parameters:**
- None
**Returns:**
- (string)
**Example:**
```
echo $response->getBody();
```
### send
**Description:**
Sends response.
**Parameters:**
- None
**Returns:**
- (void)
**Example:**
```
$response->send();
```
### sendJson
**Description:**
Sets Content-Type as `application/json`, and converts the given array to the JSON encoded body.
**Parameters:**
- `$array` (array)
**Returns:**
- (void)
**Example:**
```
$response->sendJson([
'results' => [
'user_id' => 5,
'username' => 'some_username'
],
'status' => 'OK'
]);
```
### redirect
**Description:**
Redirects to a given URL using a given status code.
**Parameters:**
- `$url` (string)
- `$status = 302` (int): HTTP status code to return**Returns:**
- (void)
**Throws:**
- `Bayfront\HttpResponse\InvalidStatusCodeException`
**Example:**
```
use Bayfront\HttpResponse\InvalidStatusCodeException;
use Bayfront\HttpResponse\Response;$response = new Response();
try {
$response->redirect('https://www.google.com', 301);
} catch (InvalidStatusCodeException $e) {
die($e->getMessage());
}
```