PHP code example of leocavalcante / request-callback
1. Go to this page and download the library: Download leocavalcante/request-callback 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/ */
leocavalcante / request-callback example snippets
use Swoole\Http\RequestCallback;
$callback = new RequestCallback(\Psr\Http\Server\RequestHandlerInterface);
use Laminas\Diactoros\Response\TextResponse;
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
use Psr\Http\Server\RequestHandlerInterface;
use Swoole\Http\{RequestCallback, Server};
$server = new Server('localhost', 9501);
$server->on('request', new RequestCallback(new class implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new TextResponse(sprintf('Hello, %s!', $request->getQueryParams()['name'] ?? 'World'));
}
}));
$server->start();
$server->on('request', RequestCallback::fromCallable(static function (ServerRequestInterface $request): ResponseInterface {
return new TextResponse(sprintf('Hello, %s!', $request->getQueryParams()['name'] ?? 'World'));
}));