PHP code example of thruster / http-server

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

    

thruster / http-server example snippets


use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Thruster\Component\EventLoop\EventLoop;
use Thruster\Component\Socket\Server;
use Thruster\Component\HttpServer\HttpServer;
use Thruster\Component\ServerApplication\SynchronousServerApplication;


$application = new class extends SynchronousServerApplication {
    /**
     * {@inheritDoc}
     */
    public function processRequestSynchronously(ServerRequestInterface $request) : ResponseInterface
    {
        $response = new Response(200);
        $response->getBody()->write('Hello World!');

        return $response;
    }

    public function preloadApplication()
    {
    }
};

$loop   = new EventLoop();
$socket = new Server($loop);

$httpServer = HttpServer::create($application)
    ->attachTo($socket)
    ->enableDebug();

$socket->listen(1337, '0.0.0.0');

$loop->run();