Skip to content

HTTP Package

PSR-7 HTTP message implementation.

Installation

bash
composer require lighthouse/http

Request

php
use Lighthouse\Http\ServerRequest;

// From globals
$request = ServerRequest::fromGlobals();

// Access data
$method = $request->getMethod();           // GET, POST, etc.
$uri = $request->getUri();                 // UriInterface
$path = $request->getUri()->getPath();     // /users/123
$query = $request->getQueryParams();       // ['page' => '1']
$body = $request->getParsedBody();         // POST data
$headers = $request->getHeaders();         // All headers
$header = $request->getHeaderLine('Host'); // Single header

Response

php
use Lighthouse\Http\Response;

// Basic response
$response = new Response(200);
$response->getBody()->write('Hello, World!');

// With status
$response = new Response(404);
$response = new Response(201);

// With headers
$response = $response->withHeader('Content-Type', 'application/json');
$response = $response->withHeader('X-Custom', 'value');

URI

php
use Lighthouse\Http\Uri;

$uri = new Uri('https://example.com/path?query=value#fragment');

$uri->getScheme();    // https
$uri->getHost();      // example.com
$uri->getPath();      // /path
$uri->getQuery();     // query=value
$uri->getFragment();  // fragment

Stream

php
use Lighthouse\Http\Stream;

// From string
$stream = Stream::create('Hello, World!');

// From file
$stream = Stream::createFromFile('/path/to/file.txt', 'r');

// Write to stream
$stream->write('More content');

// Read
$contents = $stream->getContents();

PSR-7 Compliance

All classes implement PSR-7 interfaces:

  • ServerRequestServerRequestInterface
  • ResponseResponseInterface
  • UriUriInterface
  • StreamStreamInterface
  • UploadedFileUploadedFileInterface

Released under the MIT License.