PHPackages                             amphp/http-server - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. amphp/http-server

ActiveLibrary[HTTP &amp; Networking](/categories/http)

amphp/http-server
=================

A non-blocking HTTP application server for PHP based on Amp.

v3.4.4(3mo ago)1.3k4.5M—3.7%102[11 issues](https://github.com/amphp/http-server/issues)[1 PRs](https://github.com/amphp/http-server/pulls)20MITPHPPHP &gt;=8.1CI passing

Since Nov 26Pushed 1w ago56 watchersCompare

[ Source](https://github.com/amphp/http-server)[ Packagist](https://packagist.org/packages/amphp/http-server)[ Docs](https://github.com/amphp/http-server)[ GitHub Sponsors](https://github.com/amphp)[ RSS](/packages/amphp-http-server/feed)WikiDiscussions 3.x Synced 1mo ago

READMEChangelog (10)Dependencies (21)Versions (71)Used By (20)Security (2)

amphp/http-server
=================

[](#amphphttp-server)

AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. This package provides a non-blocking, concurrent HTTP/1.1 and HTTP/2 application server for PHP based on [Revolt](https://revolt.run/). Several features are provided in separate packages, such as the [WebSocket component](https://github.com/amphp/websocket-server).

Features
--------

[](#features)

- [Static file serving](https://github.com/amphp/http-server-static-content)
- [WebSockets](https://github.com/amphp/websocket-server)
- [Dynamic app endpoint routing](https://github.com/amphp/http-server-router)
- [Request body parser](https://github.com/amphp/http-server-form-parser)
- [Sessions](https://github.com/amphp/http-server-session)
- Full TLS support
- Customizable GZIP compression
- Supports HTTP/1.1 and HTTP/2
- Middleware hooks
- [CORS](https://github.com/labrador-kennel/http-cors) (3rd party)

Requirements
------------

[](#requirements)

- PHP 8.1+

Installation
------------

[](#installation)

This package can be installed as a [Composer](https://getcomposer.org/) dependency.

```
composer require amphp/http-server
```

Additionally, you might want to install the `nghttp2` library to take advantage of FFI to speed up and reduce the memory usage.

Usage
-----

[](#usage)

This library provides access to your application through the HTTP protocol, accepting client requests and forwarding those requests to handlers defined by your application which will return a response.

Incoming requests are represented by `Request` objects. A request is provided to an implementor of `RequestHandler`, which defines a `handleRequest()` method returning an instance of `Response`.

```
public function handleRequest(Request $request): Response
```

Request handlers are covered in greater detail in the [`RequestHandler` section](#requesthandler).

This HTTP server is built on top of [the Revolt event-loop](https://revolt.run) and [the non-blocking concurrency framework Amp](https://amphp.org/amp). Thus it inherits full support of all their primitives and it is possible to use all the non-blocking libraries built on top of Revolt.

> **Note**In general, you should make yourself familiar with [the `Future` **concept**](https://amphp.org/amp#future), with [coroutines](https://amphp.org/amp#coroutines), and be aware of the several [combinator](https://amphp.org/amp#combinators) functions to really succeed at using the HTTP server.

### Blocking I/O

[](#blocking-io)

Nearly every built-in function of PHP is doing blocking I/O, that means, the executing thread (mostly equivalent to the process in the case of PHP) will effectively be halted until the response is received. A few examples of such functions: `mysqli_query`, `file_get_contents`, `usleep` and many more.

A good rule of thumb is: Every built-in PHP function doing I/O is doing it in a blocking way, unless you know for sure it doesn't.

There are [libraries providing implementations that use non-blocking I/O](https://amphp.org/packages). You should use these instead of the built-in functions.

We cover the most common I/O needs, such as [network sockets](https://github.com/amphp/socket), [file access](https://github.com/amphp/file), [HTTP requests](https://github.com/amphp/http-client) and [websockets](http://github.com/amphp/websocket-client), [MySQL](https://github.com/amphp/mysql) and [Postgres](http://github.com/amphp/postgres) database clients, and [Redis](https://github.com/amphp/redis). If using blocking I/O or long computations are necessary to fulfill a request, consider using the [Parallel library](https://github.com/amphp/parallel) to run that code in a separate process or thread.

> **Warning**Do not use any blocking I/O functions in the HTTP server.

```
// Here's a bad example, DO NOT do something like the following!

$handler = new ClosureRequestHandler(function () {
    sleep(5); // Equivalent to a blocking I/O function with a 5 second timeout

    return new Response;
});

// Start a server with this handler and hit it twice.
// You'll have to wait until the 5 seconds are over until the second request is handled.
```

### Creating an HTTP Server

[](#creating-an-http-server)

Your application will be served by an instance of `HttpServer`. This library provides `SocketHttpServer`, which will be suitable for most applications, built on components found in this library and in [`amphp/socket`](https://github.com/amphp/socket).

To create an instance of `SocketHttpServer` and listen for requests, minimally four things are required:

- an instance of [`RequestHandler`](#requesthandler) to respond to incoming requests,
- an instance of [`ErrorHander`](#errorhandler) to provide responses to invalid requests,
- an instance of `Psr\Log\LoggerInterface`, and
- at least one host+port on which to listen for connections.

```
