Library for create http servers.
- Run server with hello world page.
use php\http\{HttpServer, HttpServerRequest, HttpServerResponse};
// create server at 8888 port, 127.0.0.1 host.
$server = new HttpServer(8888, '127.0.0.1'); // port & host.
// add route with method + path + handler.
$server->route('GET', '/hello-world', function (HttpServerRequest $req, HttpServerResponse $res) {
$res->contentType('text/html');
$res->body('Hello, <b>World</b>');
});
// run server.
$server->run();
Check it, open http://localhost:8888/hello-world in your browser.
- Template routing.
use php\http\{HttpServer, HttpServerRequest, HttpServerResponse};
// create server at 8888 port, 127.0.0.1 host.
$server = new HttpServer(8888, '127.0.0.1'); // port & host.
// add route with method + path + handler.
$server->route('GET', '/hello/{name}', function (HttpServerRequest $req, HttpServerResponse $res) {
$name = $req->attribute('name');
$res->contentType('text/html');
$res->body("Hello, <b>$name</b>.");
});
// run server.
$server->run();
Check it, open http://localhost:8888/hello/YourName in your browser.
- Start server in background:
$server->runInBackground(); // run in background thread.
- Stop server, check is running.
if ($server->isRunning()) {
$server->shutdown();
}