PHP code example of luzrain / phprunner

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

    

luzrain / phprunner example snippets


// server.php

use Amp\Http\Server\HttpErrorException;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
use Luzrain\PHPStreamServer\Plugin\HttpServer\HttpServerModule;
use Luzrain\PHPStreamServer\Plugin\HttpServer\Listen;
use Luzrain\PHPStreamServer\Server;
use Luzrain\PHPStreamServer\WorkerProcess;

$server = new Server();

$server->addWorkerProcess(new WorkerProcess(
    name: 'HTTP Server',
    onStart: function (WorkerProcess $worker) {
        $requestHandler = new ClosureRequestHandler(function (Request $request) : Response {
            return match ($request->getUri()->getPath()) {
                '/' => new Response(body: 'Hello world'),
                '/ping' => new Response(body: 'pong'),
                default => throw new HttpErrorException(404),
            };
        });

        $worker->startWorkerModule(new HttpServerModule(
            listen: new Listen(listen: '0.0.0.0:8087'),
            requestHandler: $requestHandler,
        ));
    },
));

exit($server->run());
bash
$ php server.php start