1. Go to this page and download the library: Download spacetab-io/amphp-support 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/ */
spacetab-io / amphp-support example snippets
use Amp\Http\{Server\HttpServer, Server\Request, Server\Response, Server\Router, Status};
use Amp\{Loop, Promise, Socket};
use HarmonyIO\Validation\Rule\{Combinator\All, Text\AlphaNumeric, Text\LengthRange};
use Spacetab\AmphpSupport\Handler\AbstractTrustedRequest;
use Spacetab\AmphpSupport\Server\ErrorHandler;
use Spacetab\AmphpSupport\Middleware\{AcceptJsonBody, ExceptionMiddleware};
use function Amp\call;
Loop::run(function () {
$sockets = [
Socket\Server::listen('0.0.0.0:8081'),
];
$router = new Router();
$handler = new class extends AbstractTrustedRequest {
public function handleRequest(Request $request): Promise {
var_dump($this->getTrustedBody());
return call(fn() => new Response(Status::OK, [], '{"message": "hey!"}'));
}
};
$router->stack(new ExceptionMiddleware());
$router->addRoute('POST', '/', $handler, new class extends AcceptJsonBody {
public function validate(): iterable {
yield 'username' => new All(new LengthRange(3, 15), new AlphaNumeric());
}
});
$server = new HttpServer($sockets, $router, new \Psr\Log\NullLogger());
$server->setErrorHandler(new ErrorHandler());
yield $server->start();
// Stop the server gracefully when SIGINT is received.
Loop::onSignal(SIGINT, function (string $watcherId) use ($server) {
Loop::cancel($watcherId);
yield $server->stop();
});
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.