1.1. Sapien

Sapien provides server API (SAPI) object-oriented alternatives to the PHP request superglobals and global response functions.

The Sapien Request and Response objects are not HTTP request objects and responses per se. Instead, they are collection points and buffers for existing PHP variables and functions.

1.1.1. Request

The Sapien Request object encapsulates superglobals ...

Instead of using ...                    ... use Sapien\Request:
--------------------------------------- ---------------------------------------
$_COOKIE                                $request->cookies
$_GET                                   $request->query
$_GET['key'] ?? 'default'               $request->query['key'] ?? 'default'
$_FILES                                 $request->files
                                          and
                                        $request->uploads
$_POST                                  $request->input
$_SERVER                                $request->server
$_SERVER['HTTP_HEADER_NAME']            $request->headers['header-name']
$_SERVER['REQUEST_METHOD']              $request->method->name

... and content:

Instead of reading ...                  ... read from Sapien\Request:
--------------------------------------- ---------------------------------------
file_get_contents(php://input)          $request->content->body
$_SERVER['CONTENT_TYPE']                $request->content->type
                                          and
                                        $request->content->charset
$_SERVER['CONTENT_LENGTH']              $request->content->length
$_SERVER['HTTP_CONTENT_MD5']            $request->content->md5

Find out more about the Sapien Request object here.

1.1.2. Response Building and Sending

The Sapien Response object buffers all headers, cookies, and content ...

Instead of calling ...                  ... call Sapien\Response:
--------------------------------------- ---------------------------------------
header('HTTP/1.1', true, 200)           $response->setVersion('1.1')
                                          and
                                        $response->setCode(200)
header('foo: bar', true);               $response->setHeader('foo', 'bar')
header('foo: baz', false);              $response->addHeader('foo', 'baz')
setcookie('foo', 'bar');                $response->setCookie('foo', 'bar')
setrawcookie('foo', 'bar');             $response->setRawCookie('foo', 'bar')
echo $content;                          $response->setContent($content)

... and you can send the completed Response with $response->send().

Find out more about the Sapien Response object here.