igniphp / network
PHP swoole based http server
Installs: 1 007
Dependents: 1
Suggesters: 0
Security: 0
Stars: 8
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=7.1.0
- ext-swoole: >=4.0.0
- igniphp/exception: >=1.0
- psr/http-factory: >=1.0
- psr/http-message: >=1.0
- psr/http-server-middleware: >=1.0
- psr/log: >=1.0
- zendframework/zend-diactoros: >=1.7
- zendframework/zend-httphandlerrunner: ^1.0
Requires (Dev)
- mockery/mockery: >=1.0.0
- phpstan/phpstan: >=0.9.2
- phpunit/phpunit: >=7.0.0
This package is not auto-updated.
Last update: 2024-11-02 10:21:41 UTC
README
Requirements
- PHP 7.1 or better
- Swoole extension is required for network server to work
Installation
Linux users:
pecl install swoole
composer install igniphp/network
Mac users with homebrew:
brew install swoole
composer install igniphp/network
or:
brew install homebrew/php/php71-swoole
composer install igniphp/network
Basic Usage
<?php // Autoloader. require_once __DIR__ . '/vendor/autoload.php'; // Create server instance. $server = new \Igni\Network\Server(); $server->start();
Listeners
Igni http server uses event-driven model that makes it easy to scale and extend.
There are five type of events available, each of them extends Igni\Network\Server\Listener
interface:
Igni\Network\Server\OnStartListener
fired when server startsIgni\Network\Server\OnStopListener
fired when server stopsIgni\Network\Server\OnConnectListener
fired when new client connects to the serverIgni\Network\Server\OnCloseListener
fired when connection with the client is closedIgni\Network\Server\OnRequestListener
fired when new request is dispatched
<?php // Autoloader. require_once __DIR__ . '/vendor/autoload.php'; use Igni\Network\Server\Client; use Igni\Network\Server\OnRequestListener; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; use Igni\Network\Http\Stream; // Create server instance. $server = new \Igni\Network\Server(); // Each request will retrieve 'Hello' response $server->addListener(new class implements OnRequestListener { public function onRequest(Client $client, ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { return $response->withBody(Stream::fromString("Hello world")); } }); $server->start();
Configuration
Server can be easily configured with Igni\Network\Server\Configuration
class.
Please consider following example:
<?php // Autoloader. require_once __DIR__ . '/vendor/autoload.php'; // Listen on localhost at port 80. $configuration = new \Igni\Network\Server\Configuration('0.0.0.0', 80); // Create server instance. $server = new \Igni\Network\Server($configuration); $server->start();
Enabling ssl support
<?php // Autoloader. require_once __DIR__ . '/vendor/autoload.php'; $configuration = new \Igni\Network\Server\Configuration(); $configuration->enableSsl($certFile, $keyFile); // Create server instance. $server = new \Igni\Network\Server($configuration); $server->start();
Running server as a daemon
<?php // Autoloader. require_once __DIR__ . '/vendor/autoload.php'; $configuration = new \Igni\Network\Server\Configuration(); $configuration->enableDaemon($pidFile); // Create server instance. $server = new \Igni\Network\Server($configuration); $server->start();
More examples can be found in the ./examples/
directory.