It's a simple HTTP request handling library, written in PHP. build on top of PHP 7.4.12 .
download exapmle with libary from here.
or
download the latest version of the library.
require_once 'PATH_OF_LIBRARY/http.php';
$app = new Http();
$app->get('/', function (Request $request, Response $response) {
$response->send('Hello World!');
});
Depending on the request method currentlly available class functions are:
get, post, put, delete, route.
// $app->get('/path', function());
// $app->post('/path', function());
// $app->put('/path', function());
// $app->delete('/path', function());
$app->get('/', function (Request $request, Response $response) {
$response->send('Hello World!');
});
// $app->route('method', '/path', function());
$app->route('GET','/', function (Request $request, Response $response) {
$response->send('Hello World!');
});
This function is usefull to create nested routes.
// $app->router('/path', function() {
$app->get('/items', function ($uri) {
$items = new HTTP_PHP();
$items->get('/', function ($uri) {
$items->send('Sending all items');
});
$items->get('/:id', function ($uri) {
$items->send('send item');
});
$items->post('/:id', function ($uri) {
$items->send('create item');
});
$items->put('/:id', function ($uri) {
$items->send('update item');
});
$items->delete('/:id', function ($uri) {
$items->send('delete item');
});
});
// body(); returns the body of the request as a array
// body('key'); returns the value of the key
$app->get('/', function (Request $request, Response $response) {
$body = $request->body();
$response->json($body);
});
// request query string : ?key=value
// result : ["key" => "value"]
// query(); returns the query of the request as a array
// query('key'); returns the value of the key
$app->get('/', function (Request $request, Response $response) {
$query = $request->query();
$response->json($query);
});
// request params : /1234
// result : ["key"=>"1234"]
// params(); returns the params of the request as a array
// params('key'); returns the value of the key
$app->get('/:key', function (Request $request, Response $response) {
$params = $request->params();
$response->json($params);
});
$app->get('/', function (Request $request, Response $response) {
$response->send('Hello World!');
});
// jsson(); array as input
// json(); returns the body to client as json
$app->get('/', function (Request $request, Response $response) {
$body = ['key' => 'value'];
$response->json($body);
});
This function is usefull to render a view. sample file for views. file path reference use views/footer.html
instead of views\footer.html
. list of special php characters
$app->get('/page1', function (Request $request, Response $response) {
$templates=["views/header.html",
"views/page1.html",
"views/footer.html"];
$data = ["page_title" => "Page 1",
"page_content" => "Content of page 1"];
// view([templates_path_array], [data_array])
$response->view($templates, $data);
});
This project is open-source, you can contribute to it by making a pull request or opening an issue.
Author of HTTP PHP is Chayan Sarkar