1. Go to this page and download the library: Download amphp/http-server 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/ */
amphp / http-server example snippets
public function handleRequest(Request $request): Response
// Here's a bad example, DO NOT do something like the following!
$handler = new ClosureRequestHandler(function () {
sleep(5); // Equivalent to a blocking I/O function with a 5 second timeout
return new Response;
});
// Start a server with this handler and hit it twice.
// You'll have to wait until the 5 seconds are over until the second request is handled.
use Amp\ByteStream;
use Amp\Http\HttpStatus;
use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\SocketHttpServer;
use Amp\Log\ConsoleFormatter;
use Amp\Log\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
stHandler {
public function handleRequest(Request $request) : Response
{
return new Response(
status: HttpStatus::OK,
headers: ['Content-Type' => 'text/plain'],
body: 'Hello, world!',
);
}
};
$errorHandler = new DefaultErrorHandler();
$server = SocketHttpServer::createForDirectAccess($logger);
$server->expose('127.0.0.1:1337');
$server->start($requestHandler, $errorHandler);
// Serve requests until SIGINT or SIGTERM is received by the process.
Amp\trapSignal([SIGINT, SIGTERM]);
$server->stop();
public function handleRequest(Request $request): Response
public function handleRequest(Request $request, RequestHandler $next): Response
function stackMiddleware(RequestHandler $handler, Middleware ...$middleware): RequestHandler
$requestHandler = new class implements RequestHandler {
public function handleRequest(Request $request): Response
{
return new Response(
status: HttpStatus::OK,
headers: ["content-type" => "text/plain; charset=utf-8"],
body: "Hello, World!",
);
}
}
$middleware = new class implements Middleware {
public function handleRequest(Request $request, RequestHandler $next): Response
{
$requestTime = microtime(true);
$response = $next->handleRequest($request);
$response->setHeader("x-request-time", microtime(true) - $requestTime);
return $response;
}
};
$stackedHandler = Middleware\stackMiddleware($requestHandler, $middleware);
$errorHandler = new DefaultErrorHandler();
// $logger is a PSR-3 logger instance.
$server = SocketHttpServer::createForDirectAccess($logger);
$server->expose('127.0.0.1:1337');
$server->start($stackedHandler, $errorHandler);
public function handleError(
int $status,
?string $reason = null,
?Request $request = null,
): Response
/**
* @param string $method The HTTP method verb.
* @param array<string>|array<string, array<string>> $headers An array of strings or an array of string arrays.
*/
public function __construct(
private readonly Client $client,
string $method,
Psr\Http\Message\UriInterface $uri,
array $headers = [],
Amp\ByteStream\ReadableStream|string $body = '',
private string $protocol = '1.1',
?Trailers $trailers = null,
)
public function getClient(): Client
public function getMethod(): string
public function setMethod(string $method): void
public function getUri(): Psr\Http\Message\UriInterface
public function setUri(Psr\Http\Message\UriInterface $uri): void
public function getProtocolVersion(): string
public function setProtocolVersion(string $protocol)
/** @return array<non-empty-string, list<string>> */
public function getHeaders(): array
public function hasHeader(string $name): bool
/** @return list<string> */
public function getHeaderArray(string $name): array
public function getHeader(string $name): ?string
public function setHeaders(array $headers): void
/** @param array<string>|string $value */
public function setHeader(string $name, array|string $value): void
/** @param array<string>|string $value */
public function addHeader(string $name, array|string $value): void
public function removeHeader(string $name): void
public function getBody(): RequestBody
public function setBody(ReadableStream|string $body)
/** @return array<non-empty-string, RequestCookie> */
public function getCookies(): array
public function getCookie(string $name): ?RequestCookie
public function setCookie(RequestCookie $cookie): void
public function removeCookie(string $name): void
public function getAttributes(): array
public function removeAttributes(): array
public function hasAttribute(string $name): bool
public function getAttribute(string $name): mixed
public function setAttribute(string $name, mixed $value): void
public function removeAttribute(string $name): void
public function getTrailers(): Trailers
public function setTrailers(Trailers $trailers): void
/**
* @param int $code The HTTP response status code.
* @param array<string>|array<string, array<string>> $headers An array of strings or an array of string arrays.
*/
public function __construct(
int $code = HttpStatus::OK,
array $headers = [],
Amp\ByteStream\ReadableStream|string $body = '',
?Trailers $trailers = null,
)
public function getBody(): Amp\ByteStream\ReadableStream
public function setBody(Amp\ByteStream\ReadableStream|string $body)
/** @return array<non-empty-string, list<string>> */
public function getHeaders(): array
public function hasHeader(string $name): bool
/** @return list<string> */
public function getHeaderArray(string $name): array
public function getHeader(string $name): ?string
public function setHeaders(array $headers): void
/** @param array<string>|string $value */
public function setHeader(string $name, array|string $value): void
/** @param array<string>|string $value */
public function addHeader(string $name, array|string $value): void
public function removeHeader(string $name): void
public function getStatus(): int
public function getReason(): string
public function setStatus(int $code, string | null $reason): void
/** @return array<non-empty-string, ResponseCookie> */
public function getCookies(): array
public function getCookie(string $name): ?ResponseCookie
public function setCookie(ResponseCookie $cookie): void
public function removeCookie(string $name): void
/** @return array<string, Push> Map of URL strings to Push objects. */
public function getPushes(): array
/** @param array<string>|array<string, array<string>> $headers */
public function push(string $url, array $headers): void
public function isUpgraded(): bool
/** @param Closure(Driver\UpgradedSocket, Request, Response): void $upgrade */
public function upgrade(Closure $upgrade): void
public function getUpgradeCallable(): ?Closure
/** @param Closure():void $onDispose */
public function onDispose(Closure $onDispose): void
public function getTrailers(): Trailers
public function setTrailers(Trailers $trailers): void
public function __construct(
ReadableStream|string $stream,
?Closure $upgradeSize = null,
)
public function increaseSizeLimit(int $limit): void