PHPackages                             amphp/http - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. amphp/http

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

amphp/http
==========

Basic HTTP primitives which can be shared by servers and clients.

v2.1.2(1y ago)1038.5M—4.2%10[1 issues](https://github.com/amphp/http/issues)[2 PRs](https://github.com/amphp/http/pulls)20MITPHPPHP &gt;=8.1CI passing

Since Jan 22Pushed 2w ago7 watchersCompare

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

READMEChangelog (10)Dependencies (8)Versions (31)Used By (20)Security (2)

amphp/http
==========

[](#amphphttp)

AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. `amphp/http` is a collection of basic HTTP primitives which can be shared by servers and clients.

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

[](#installation)

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

```
composer require amphp/http
```

This package requires PHP 8.1 or later.

Usage
-----

[](#usage)

This package provides basic primitives needed for HTTP clients and servers.

- [Status codes](#status-codes)
- [Cookies](#cookies)
- [Headers](#headers)

### Status Codes

[](#status-codes)

HTTP status codes are made human-readable via `Amp\Http\HttpStatus`. It includes a constant for each IANA registered status code. Additionally, a default reason is available via `HttpStatus::getReason($code)`.

### Cookies

[](#cookies)

HTTP cookies are specified by [RFC 6265](https://tools.ietf.org/html/rfc6265). This package implements parsers for the `set-cookie` and `cookie` headers. It further has a developer friendly API for creating such headers.

> **Note**This library doesn't set standards regarding the cookie encoding. As such, the limitations of RFC 6265 apply to names and values. If you need to set arbitrary values for certain cookies, it's recommended to use an encoding mechanism like URL encoding or Base64.

#### Set-Cookie

[](#set-cookie)

The `set-cookie` header is used to create cookies. Servers send this header in responses and clients parse the headers if a response contains such headers. Every header contains exactly one header. Hence, the responsible class is called `ResponseCookie`.

> **Note**More information about `set-cookie` can be obtained from the [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) or other sources.

`ResponseCookie::fromHeader()` accepts a header value and attempts to parse it. If the parsing succeeds, a `ResponseCookie` is returned. If not, `null` is returned. No exceptions are thrown, because received cookies are always user input and untrusted and malformed headers should be discarded according to the RFC.

```
$attributes = CookieAttributes::default()->withSecure();
$cookie = new ResponseCookie("session", \bin2hex(\random_bytes(16)), $attributes);

var_dump($cookie->getName());
var_dump($cookie->getValue());
var_dump($cookie->isHttpOnly());
var_dump("set-cookie: " . $cookie);
```

```
string(7) "session"
string(32) "7b6f532a60bc0786fdfc42307649d634"
bool(true)
string(70) "set-cookie: session=7b6f532a60bc0786fdfc42307649d634; Secure; HttpOnly"

```

#### Cookie

[](#cookie)

The `cookie` header is used to send cookies from a client to a server. Clients send this header in requests and servers parse the header if a request contains such a header. Clients must not send more than one such header. Hence, the responsible class is called `RequestCookie`.

> **Note**More information about `cookie` can be obtained from the [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie) or other sources.

`RequestCookie::fromHeader()` accepts a header value and attempts to parse it. If the parsing succeeds, an array of `RequestCookie` instances is returned. If not, an empty array is returned. No exceptions are thrown, because received cookies are always user input and untrusted and malformed headers should be discarded according to the RFC.

```
$responseCookie = new ResponseCookie("session", \bin2hex(\random_bytes(16)), $attributes);

$cookie = ResponseCookie::fromHeader($responseCookie);
$cookie = new RequestCookie("session", $cookie->getValue());

var_dump($cookie->getName());
var_dump($cookie->getValue());
var_dump("cookie: " . $cookie);
```

```
string(7) "session"
string(32) "7b6f532a60bc0786fdfc42307649d634"
string(48) "cookie: session=7b6f532a60bc0786fdfc42307649d634"

```

### Headers

[](#headers)

This package provides an HTTP header parser based on [RFC 7230](https://tools.ietf.org/html/rfc7230). It also provides a corresponding header formatter.

#### Parsing Headers

[](#parsing-headers)

`Amp\Http\Rfc7230::parseHeaders()` parses raw headers into an array mapping header names to arrays of header values. Every header line must end with `\r\n`, also the last one.

```
