PHP code example of edgaralexanderfr / php-espresso
1. Go to this page and download the library: Download edgaralexanderfr/php-espresso 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/ */
edgaralexanderfr / php-espresso example snippets
spresso\Http\Request;
use Espresso\Http\Response;
use Espresso\Http\Router;
use Espresso\Http\Server;
const PORT = 80;
$server = new Server();
$router = new Router();
$router->get('/', function (Request $request, Response $response) {
return $response->send([
'message' => 'Hello world!',
'code' => 200,
]);
});
$server->use($router);
$server->listen(PORT, function () use ($server) {
$server->log('Listening at port ' . PORT . '...');
});
spresso\Http\Request;
use Espresso\Http\Response;
use Espresso\Http\Router;
use Espresso\Http\Server;
$server = new Server();
$router = new Router();
$router->get('/php-espresso-page', function (Request $request, Response $response) {
return $response->setPayload(
<<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Web Page with PHP Espresso!</title>
</head>
<body>
<h1>My Web Page with PHP Espresso!</h1>
<p>This page was served using PHP Espresso.</p>
</body>
</html>
HTML
);
});
$server->use($router);
$server->listen(80, function () use ($server) {
$server->log('Listening at port 80...');
});
spresso\Http\Request;
use Espresso\Http\Response;
use Espresso\Http\Router;
use Espresso\Http\Server;
$server = new Server();
$router = new Router();
$router->post('/users', function (Request $request, Response $response) {
$body_json = $request->getJSON();
return $response->send([
'message' => 'User created successfully',
'code' => 201,
'user' => $body_json,
], 201);
});
$server->use($router);
$server->listen(80, function () use ($server) {
$server->log('Listening at port 80...');
});
spresso\Http\Request;
use Espresso\Http\Response;
use Espresso\Http\Router;
use Espresso\Http\Server;
/** @var stdClass[] */
$users = [];
/** @var int */
$users_id = 1;
$server = new Server();
$router = new Router();
$router->get('/users', function (Request $request, Response $response) use (&$users) {
return $response->send($users);
});
$router->get('/users/:id', function (Request $request, Response $response) use (&$users) {
$id = $request->getId();
foreach ($users as $user) {
if (isset($user->{'id'}) && $user->id == $id) {
return $response->send($user);
}
}
return $response->send([
'message' => 'User not found',
'code' => 404,
], 404);
});
$router->post('/users', function (Request $request, Response $response) use (&$users, &$users_id) {
$body = $request->getJSON();
$email = $body->email ?? null;
$name = $body->name ?? null;
if (!$email || !$name) {
return $response->send([
'message' => 'Email and Name are est, Response $response) use (&$users) {
$id = $request->getId();
foreach ($users as $i => &$user) {
if (isset($user->{'id'}) && $user->id == $id) {
array_splice($users, $i, 1);
return $response->send([
'message' => 'User deleted successfully',
'code' => 200,
]);
}
}
return $response->send([
'message' => 'User not found',
'code' => 404,
], 404);
});
$server->use($router);
$server->listen(80, function () use ($server) {
$server->log('Listening at port 80...');
});
spresso\Http\Request;
use Espresso\Http\Response;
use Espresso\Http\Router;
use Espresso\Http\Server;
define('AUTH_CREDENTIALS', (object) [
'user' => '[email protected]',
'pass' => '1234567890', // Please... don't...
]);
/**
* Middleware for admin authentication.
*/
function auth(Request $request, Response $response, callable $next)
{
$authorization = $request->getHeader('Authorization') ?? '';
$auth = explode(' ', $authorization);
$type = $auth[0] ?? '';
$token = $auth[1] ?? '';
$credentials = explode(':', base64_decode($token));
$user = $credentials[0] ?? null;
$pass = $credentials[1] ?? null;
if ($type != 'Bearer' || $user != AUTH_CREDENTIALS->user || $pass != AUTH_CREDENTIALS->pass) {
return $response->send([
'message' => Espresso\Http\CODES[401],
'code' => 401,
], 401);
}
$next();
}
/** @var stdClass[] */
$users = [];
/** @var int */
$users_id = 1;
$server = new Server();
$router = new Router();
// Global middleware to check service status:
$server->use(function (Request $request, Response $response, callable $next) use ($argv) {
$status = $argv[1] ?? '';
if ($status == 'service-closed') {
return $response->send([
'message' => 'Service unavailable temporary due to maintenance',
'code' => 503,
], 503);
}
$next();
});
$router->get('/users', function (Request $request, Response $response) use (&$users) {
return $response->send($users);
});
$router->post('/users', 'auth', function (Request $request, Response $response) use (&$users, &$users_id) {
$body = $request->getJSON();
$email = $body->email ?? null;
$name = $body->name ?? null;
if (!$email || !$name) {
return $response->send([
'message' => 'Email and Name are
unction Espresso\Event\async;
use Espresso\Http\Request;
use Espresso\Http\Response;
use Espresso\Http\Router;
use Espresso\Http\Server;
const SMALLER_FILE_PATH = __DIR__ . '/files/smaller-file.txt';
const BIGGER_FILE_PATH = __DIR__ . '/files/bigger-file.txt';
function read_file(string $path, int $bytes, callable $callable = null): void
{
$file = fopen($path, 'r');
$file_size = filesize($path);
$content = '';
$read_bytes = 0;
async(function () use ($bytes, $callable, &$file, $file_size, &$content, &$read_bytes) {
if ($read_bytes < $file_size) {
$chunk_size = min($file_size - $read_bytes, $bytes);
$chunk = fread($file, $chunk_size);
$content .= $chunk;
$read_bytes += $chunk_size;
return false;
}
if ($callable) {
$callable($content);
}
});
}
$server = new Server();
$router = new Router();
$router->get('/read-file', function (Request $request, Response $response, callable $next) {
$size = $request->getParam('size');
$file_path = $size == 'big' ? BIGGER_FILE_PATH : SMALLER_FILE_PATH;
read_file($file_path, 8, function (string $content) use ($request, $response, $next, $size) {
$response->send([
'file_content' => $content,
'size' => $size,
]);
$next();
});
});
$server->use($router);
$server->async(true);
$server->listen(80, function () use ($server) {
$server->log('Listening at port 80...');
});
bash
composer
bash
php server.php # Use sudo if necessary for port 80