- PHP 7.1 or better
- Swoole extension is required for network server to work
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
<?php// Autoloader.require_once__DIR__ . '/vendor/autoload.php';
// Create server instance.$server = new \Igni\Network\Server();
$server->start();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\OnStartListenerfired when server startsIgni\Network\Server\OnStopListenerfired when server stopsIgni\Network\Server\OnConnectListenerfired when new client connects to the serverIgni\Network\Server\OnCloseListenerfired when connection with the client is closedIgni\Network\Server\OnRequestListenerfired when new request is dispatched
<?php// Autoloader.require_once__DIR__ . '/vendor/autoload.php';
useIgni\Network\Server\Client;
useIgni\Network\Server\OnRequestListener;
usePsr\Http\Message\ServerRequestInterface;
usePsr\Http\Message\ResponseInterface;
useIgni\Network\Http\Stream;
// Create server instance.$server = new \Igni\Network\Server();
// Each request will retrieve 'Hello' response$server->addListener(newclassimplements OnRequestListener {
publicfunctiononRequest(Client$client, ServerRequestInterface$request, ResponseInterface$response): ResponseInterface {
return$response->withBody(Stream::fromString("Hello world"));
}
});
$server->start();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();<?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();<?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.

