Home

Awesome

HTTP for Icicle

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 Coroutines built from Awaitables and Generators to make writing asynchronous code more like writing synchronous code.

Build Status Coverage Status Semantic Version MIT License @icicleio on Twitter

Documentation and Support

Requirements
Suggested
Installation

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.3"
    }
}

Example

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\{BasicResponse, Request, Response};
use Icicle\Http\Server\{RequestHandler, Server};
use Icicle\Socket\Socket;
use Icicle\Loop;

$server = new Server(new class implements RequestHandler {
    public function onRequest(Request $request, Socket $socket)
    {
        $response = new BasicResponse(Response::OK, [
            'Content-Type' => 'text/plain',
        ]);
        
        yield from $response->getBody()->end('Hello, world!');
        
        yield $response;
    }
    
    public function onError($code, Socket $socket)
    {
        return new BasicResponse($code);
    }
});

$server->listen(8080);

Loop\run();