Introduction to HTTP layer

The icanboogie/http package provides an API to handle HTTP requests, with representations for requests, request files, responses, and headers. A request dispatcher is also provided, that can be used with your favorite routing solution with very little effort.

The following example demonstrates how you can use a simple closure to create a Hello world! application:

<?php

use ICanBoogie\HTTP\Request;
use ICanBoogie\HTTP\RequestDispatcher;
use ICanBoogie\HTTP\Response;

require 'vendor/autoload.php';

$dispatcher = new RequestDispatcher([

    'hello world' => function(Request $request) {

        $who = $request['name'] ?: 'world';

        return new Response("Hello $who!", Response::STATUS_OK, [

            'Content-Type' => 'text/plain'

        ]);

    }

]);

$request = Request::from($_SERVER);
$response = $dispatcher($request);
$response();

Note: You might want to check the icanboogie/routing package if you require a nice router.

Exceptions

The following exceptions are defined by the HTTP package:

Exceptions defined by the package implement the ICanBoogie\HTTP\Exception interface. Using this interface one can easily catch HTTP related exceptions:

<?php

try
{
    // …
}
catch (\ICanBoogie\HTTP\Exception $e)
{
    // HTTP exception types
}
catch (\Exception $e)
{
    // Other exception types
}

Helpers

The following helpers are available:

<?php

namespace ICanBoogie\HTTP;

$request = get_initial_request();
$response = dispatch($request);