bronsted / psr-http
Psr-http
v2.0
2023-08-21 12:47 UTC
Requires
- php: ^8.1
- bronsted/fiberloop: ^2.0
- kekos/multipart-form-data-parser: ^1.0
- php-di/php-di: ^7.0
- psr/http-client: ^1.0
Requires (Dev)
- colinodell/psr-testlogger: ^1.2
- nyholm/psr7: ^1.5
- phpunit/phpunit: ^10.0
README
This is a http server and client package with PSR interfaces, so it can be used with frameworks which uses PSR interface. It is build with FiberLoop which is coroutines build on php fibers.
Install
You can install this package via Composer:
composer install bronsted\psr-http
Usage
Server usage:
<?php require __DIR__ . '/vendor/autoload.php'; use bronsted\HttpServer; use Nyholm\Psr7\Factory\Psr17Factory; use Psr\Http\Message\ServerRequestInterface; $factory = new Psr17Factory(); $server = new HttpServer('0.0.0.0', 8000, $factory, $factory, $factory, $factory, $factory); $server->run(function (ServerRequestInterface $request) use ($factory) { $response = $factory->createResponse(); $path = $request->getUri()->getPath(); if ($path == '/') { $params = $request->getQueryParams(); $response->getBody()->write('Hello ' . (empty($params) ? 'world' : $params['name'])); } else { $response = $response->withStatus(404); } return $response; });
Standalone client usage context:
<?php require __DIR__ . '/vendor/autoload.php'; use bronsted\HttpClient; use bronsted\FiberLoop; use Nyholm\Psr7\Factory\Psr17Factory; FiberLoop::instance()->defer(function() use($argv) { $factory = new Psr17Factory(); $request = $factory->createRequest('GET', $argv[1]); $client = new HttpClient($factory); $response = $client->sendRequest($request); echo $response->getStatusCode() . PHP_EOL; echo $response->getBody()->getContents() . PHP_EOL; }); FiberLoop::instance()->run();
Bugs and improvements
Report bugs in GitHub issues or feel free to make a pull request :-)
License
MIT