PHP code example of rigsto / api-http-status
1. Go to this page and download the library: Download rigsto/api-http-status library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rigsto / api-http-status example snippets
use Rigsto\ApiHttpStatus\HttpStatus;
HttpStatus::OK;
HttpStatus::NOT_FOUND;
HttpStatus::INTERNAL_SERVER_ERROR;
HttpStatus::BAD_REQUEST;
HttpStatus::UNAUTHORIZED;
use Rigsto\ApiHttpStatus\HttpStatus;
$http = HttpStatus::OK;
$http->getStatusCode(); // 200
$http->getName(); // Ok
$http->getCategory(); // Success
$http->isSuccess(); // true
$http = HttpStatus::UNAUTHORIZED;
$http->getStatusCode(); // 401
$http->getName(); // Unauthorized
$http->getCategory(); // Client Error
$http->isSuccess(); // false
$http = HttpStatus::INTERNAL_SERVER_ERROR;
$http->getStatusCode(); // 500
$http->getName(); // Internal Server Error
$http->getCategory(); // Server Error
$http->isSuccess(); // false
use Rigsto\ApiHttpStatus\HttpStatus;
$code = 200;
$codeValidity = HttpStatus::isValidCode($code); // true
$http = HttpStatus::fromCode($code); // HttpStatus::OK
$code = 999
$codeValidity = HttpStatus::isValidCode($code); // false
$http = HttpStatus::fromCode($code); // null
use Rigsto\ApiHttpStatus\HttpStatus;
use Rigsto\ApiHttpStatus\ApiResponse;
$data = [...];
$response = ApiResponse::generateResponse(HttpStatus::OK, null, $data);
// {"success": true, "code": 200, "message": "Ok", "data": [...]}
$response = ApiResponse::generateResponse(HttpStatus::BAD_REQUEST, 'Custom Message', $data);
// {"success": false, "code": 400, "message": "Custom Message", "data": [...]}
$response = ApiResponse::generateResponse(HttpStatus::INTERNAL_SERVER_ERROR, null, null);
// {"success": false, "code": 500, "message": "Internal Server Error", "data": null}
use Rigsto\ApiHttpStatus\ApiResources;
$data = [...];
$response = ApiResources::generateSuccessResponse();
// {"success": true, "code": 200, "message": "Ok", "data": null}
$response = ApiResources::generateSuccessResponse(message: 'Custom Message');
// {"success": true, "code": 200, "message": "Custom Message", "data": null}
$response = ApiResources::generateSuccessResponse(data: $data);
// {"success": true, "code": 200, "message": "Ok", "data": [...]}
use Rigsto\ApiHttpStatus\ApiResources;
$response = ApiResources::generateUnauthorizedResponse();
// {"success": false, "code": 401, "message": "Unauthorized", "data": null}
use Rigsto\ApiHttpStatus\HttpStatus;
use Rigsto\ApiHttpStatus\ApiResponse;
$data = ["your data here" => "..."];
$response = ApiResponse::generatePaginationResponse(HttpStatus::OK, null, $data);