Home

Awesome

<p align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/phpstreamserver/.github/refs/heads/main/assets/phpss_http_server_light.svg"> <img alt="PHPStreamServer logo" align="center" width="70%" src="https://raw.githubusercontent.com/phpstreamserver/.github/refs/heads/main/assets/phpss_http_server_dark.svg"> </picture> </p>

HTTP Server Plugin for PHPStreamServer

PHP >=8.2 Version Downloads

The HTTP Server Plugin for PHPStreamServer extends the core functionality by providing a high performance, asynchronous HTTP server. It works in the event loop and always persists in memory, enabling fast request handling and reducing startup overhead.

Features:

Install

$ composer require phpstreamserver/core phpstreamserver/http-server

Configure

Here is an example of a simple HTTP server configuration.

// server.php

use Amp\Http\Server\HttpErrorException;
use Amp\Http\Server\Request;
use Amp\Http\Server\Response;
use PHPStreamServer\Core\Server;
use PHPStreamServer\Plugin\HttpServer\HttpServerPlugin;
use PHPStreamServer\Plugin\HttpServer\HttpServerProcess;

$server = new Server();

$server->addPlugin(
    new HttpServerPlugin(),
);

$server->addWorker(
    new HttpServerProcess(
        name: 'Web Server',
        count: 4,
        listen: '0.0.0.0:8080',
        onStart: function (HttpServerProcess $worker): void {
            // initialization
        },
        onRequest: function (Request $request, HttpServerProcess $worker): Response {
            return match ($request->getUri()->getPath()) {
                '/' => new Response(body: 'Hello world'),
                '/ping' => new Response(body: 'pong'),
                default => throw new HttpErrorException(404),
            };
        }
    ),
);

exit($server->run());

Run

$ php server.php start