irap / http-codes
A package to make error codes easier.
Installs: 647
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
pkg:composer/irap/http-codes
Requires
- php: >=7.0
README
To use this package of defines you need to manually include the
Usage
Download this package into your project with the command:
composer require irap/http-codes
Below is an example of using the package to return a 404 JSON response for an API request.
<?php http_response_code(iRAP/Http/HttpCode::NOT_FOUND); $body = array( "result" => "error", "message" => "Resource not found.", ); header('Content-Type: application/json'); die(json_encode($body));
Alternatively...
<?php use iRAP\Http\HttpCode; http_response_code(HttpCode::NOT_FOUND); $body = array( "result" => "error", "message" => "Resource not found.", ); header('Content-Type: application/json'); die(json_encode($body));
There's also a method to check if a passed parameter is a valid http status code based on this.
<?php use iRAP\Http\HttpCode; if (HttpCode::isValid(404)) { echo "Valid HTTP status code"; } else { echo "Invalid HTTP status code"; }