PHP code example of phpstreamserver / http-server

1. Go to this page and download the library: Download phpstreamserver/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/ */

    

phpstreamserver / http-server example snippets


// 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\Worker\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());
bash
$ php server.php start