1. Go to this page and download the library: Download legionth/http-server-react 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/ */
legionth / http-server-react example snippets
$callback = function (ServerRequestInterface $request) {
$content = '<html>
<body>
<h1> Hello World! </h1>
<p> This is your own little server. Written in PHP :-) </p>
</body>
</html>';
return new Response(
200,
array(
'Content-Length' => strlen($content),
'Content-Type' => 'text/html'
),
$content
);
};
$socket = new Socket($loop);
$socket->listen(10000, 'localhost');
$server = new HttpServer($socket, $callback);
$loop->run();
$callback = function (ServerRequestInterface $request) {
return new Promise(function ($resolve, $reject) use ($request) {
$request->getBody->on('end', function () use (&$contentLength, $resolve) {
$content = '<html>
<body>
<h1> Hello World! </h1>
<p> This is your own little server. Written in PHP :-) </p>
</body>
</html>';
return new Response(
200,
array(
'Content-Length' => strlen($content),
'Content-Type' => 'text/html'
),
$content
);
});
};
}
$callback = function (ServerRequestInterface $request) {
return new Promise(function ($resolve, $reject) use ($body) {
$contentLength = 0;
$request->getBody()->on('data', function ($chunk) use ($resolve, &$contentLength) {
$contentLength += strlen($chunk);
});
$request->getBody()->on('end', function () use (&$contentLength, $resolve) {
$content = "Transferred data length: " . $contentLength ."\n";
$resolve(
new Response(
200,
array(
'Content-Length' => strlen($content),
'Content-Type' => 'text/html'
),
$content
)
);
});
});
};
$loop = React\EventLoop\Factory::create();
$callback = function (ServerRequestInterface $request) use ($loop) {
throw new Exception();
};
$socket = new Socket($loop);
$socket->listen(10000, 'localhost');
$server = new HttpServer($socket, $callback);
$loop->run();
$callback = function (ServerRequestInterface $request) {
try {
//something that could go wrong
} catch(Exception $exception) {
return new Response(500, array('Content-Length' => 5), 'error');
}
}
$httpServer = new HttpServer($socket, $callback);
$callback = function (ServerRequestInterface $request) {
return new Promise(function ($resolve, $reject) use ($request) {
$request->getBody()->on('end', function () {
$response = heavyCalculationFunction();
$resolve($response);
});
});
};
$callback = function (ServerRequestInterface $request) {
return new Response();
}
$middleware = function (ServerRequestInterface $request, callable $next) {
// check or maninpulate the request object
...
// call of next middleware chain link
return $next($request);
}
$server = new HttpServer($socket, $callback);
$server->addMiddleware($middleware);
...
$timeBlockingMiddleware = function (ServerRequestInterface $request, callable $next) {
// Will call the next middleware from 00:00 till 16:00
// otherwise an 403 Forbidden response will be sent to the client
if (((int)date('Hi') < 1600 && (int)date('Hi') > 0) {
return $next($request);
}
return new Response(403);
};
$addHeaderToRequest = function (ServerRequestInterface $request, callable $next) {
$request = $request->withAddedHeader('Date', date('Y-m-d'));
return $next($request);
};
$addHeaderToResponse = function (ServerRequestInterface $request, callable $next) {
$response = $next($request);
$response = $response->withAddedHeader('Age', '12');
return $response;
};
$server = new HttpServer($socket, $callback);
$server->addMiddleware($timeBlockingMiddleware);
$server->addMiddleware($addHeaderToRequest);
$server->addMiddleware($addHeaderToResponse);
$callback = function (ServerRequestInterface $request) {
$input = new ReadableStream();
$responseBody = new HttpBodyStream($input);
// your computation
// emit via `$input`
$promise = new Promise(function ($resolve, $reject) use ($request, $responseBody) {
$request->getBody()->on('end', function () use ($resolve, $responseBody){
$resolve(new Response(200, array(), $responseBody));
});
});
return $promise;
}
$socket = new Socket($loop);
$secureSocket = new SecureServer(
$socket,
$loop,
array('local_cert' => 'secret.pem')
);
$secureSocket->listen(10000, 'localhost');
$secureSocket->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
$server = new HttpServer($secureSocket, $callback);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.