Asynchronous, non-blocking HTTP/1.1 client and server.
This library is a component for Icicle that provides an HTTP/1.1 server and client implementations. Like other Icicle components, this library uses Promises and Generators for asynchronous operations that may be used to build Coroutines to make writing asynchronous code more like writing synchronous code.
- PHP 5.5+
- openssl extension: Required to create HTTPS servers or make requests over HTTPS.
The recommended way to install is with the Composer package manager. (See the Composer installation guide for information on installing and using Composer.)
Run the following command to use this library in your project:
composer require icicleio/http
You can also manually edit composer.json
to add this library as a project requirement.
// composer.json
{
"require": {
"icicleio/http": "^0.2"
}
}
The example below creates a simple HTTP server that responds with Hello, world!
to every request.
#!/usr/bin/env php
<?php
require '/vendor/autoload.php';
use Icicle\Http\Message\RequestInterface;
use Icicle\Http\Message\Response;
use Icicle\Http\Server\Server;
use Icicle\Loop;
$server = new Server(function (RequestInterface $request) {
$response = new Response(200);
yield $response->getBody()->end('Hello, world!');
yield $response->withHeader('Content-Type', 'text/plain');
});
$server->listen(8080);
echo "Server running at https://127.0.0.1:8080\n";
Loop\run();
More documentation coming soon...