bayfrontmedia/php-http-response

Easily send HTTP responses.

v2.0.1 2023-02-03 17:49 UTC

This package is auto-updated.

Last update: 2024-11-30 02:13:41 UTC


README

Easily send HTTP responses.

License

This project is open source and available under the MIT License.

Author

Bayfront Media

Requirements

  • PHP ^8.0
  • JSON PHP extension

Installation

composer require bayfrontmedia/php-http-response

Usage

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