PHPackages                             kambo/httpmessage - 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. kambo/httpmessage

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

kambo/httpmessage
=================

Kambo httpmessage a PSR-7 implementation

v0.9.1(8y ago)51.2k1[1 issues](https://github.com/kambo-1st/HttpMessage/issues)2MITPHPPHP &gt;=5.5

Since Aug 10Pushed 8y ago2 watchersCompare

[ Source](https://github.com/kambo-1st/HttpMessage)[ Packagist](https://packagist.org/packages/kambo/httpmessage)[ RSS](/packages/kambo-httpmessage/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (2)Dependencies (3)Versions (2)Used By (2)

kambo httpmessage
=================

[](#kambo-httpmessage)

[![Build Status](https://camo.githubusercontent.com/0c1ef3d93dd53292009669110d38dd7f46d47606e57e6849a7887ea38f2c04bd/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6b616d626f2d3173742f487474704d6573736167652e7376673f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://travis-ci.org/kambo-1st/HttpMessage)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/67053d8624755769bd53681b787c9b29ac6c7402640441ac894a97a0c3bb3b0a/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6b616d626f2d3173742f487474704d6573736167652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/kambo-1st/HttpMessage/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/ed80645094bf29608c00751607f4d8573a88e98e1603d39d63c825f5384a721a/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6b616d626f2d3173742f487474704d6573736167652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/kambo-1st/HttpMessage/)[![Dependency Status](https://camo.githubusercontent.com/9a93287f0eea8b3a6a675f2dc9fd74ba7909fa964965fc5fbbd10484ecf2bb0d/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3537363161383361306138326232303035333138326363652f62616467652e7376673f7374796c653d666c6174)](https://www.versioneye.com/user/projects/5761a83a0a82b20053182cce)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

Standalone complete implementation of PSR-7 - HTTP message interfaces.

Package is not depended on any existing framework and implements only functionality described in PSR-7.

Install
-------

[](#install)

Prefered way to install library is with composer:

```
composer require kambo/httpmessage
```

Basic usage
-----------

[](#basic-usage)

### Factories

[](#factories)

Package comes with the set of factory classes for creating instances of `Files`, `Headers`, `Uri` and `ServerRequest` classes from the super global variables (`$_POST`, `$_COOKIE`, `$_FILES` etc.). Each of object is created by calling method `create` on corresponding factory. Only parameter is instance of environment class. For example following code will create instance of `ServerRequest`:

```
// Create Environment object based on server variables.
$environment = new Environment($_SERVER, fopen('php://input', 'w+'), $_POST, $_COOKIE, $_FILES);
// Create instance of ServerRequest object
$serverRequest = (new ServerRequestFactory())->create($environment);
```

Environment object is simple wrapper around server and CGI variables and is usually instanced from the super global variables (`$_POST`, `$_COOKIE`, `$_FILES` etc.).

Constructor of `Environment` class has two mandatory parameters - `server` and `body`.

Server is an associative array containing information such as headers, paths, and script locations, it must have same structure as the super global variable `$_SERVER`. Body is `resource`, created from raw data from the request body. Usually it should be created from the `php://input` stream.

Constructor also accept three optional parameters - post, cookie and files. Each of these parameters is counterpart to super global variable and it must have same structure eg.: cookie must have same structure as the `$_COOKIE` variable. If some of these parameters is missing an empty array is used.

```
// Create Environment object based on server variables.
$environment = new Environment($_SERVER, fopen('php://input', 'w+'), $_POST, $_COOKIE, $_FILES);
```

### Server request - ServerRequest

[](#server-request---serverrequest)

Server request is representation of an incoming, server-side HTTP request.

Per the HTTP specification, this class includes properties for each of the following:

- Protocol version
- HTTP method
- URI
- Headers
- Message body

Additionally, it encapsulates all data as it has arrived to the application from the CGI and/or PHP environment, including:

- The values represented in $\_SERVER.
- Any cookies provided (generally via $\_COOKIE)
- Query string arguments (generally via $\_GET, or as parsed via parse\_str())
- Upload files, if any (as represented by $\_FILES)
- Deserialized body parameters (generally from $\_POST)

Creation of `ServerRequest` instance is done through `ServerRequestFactory` from existing super global variables ($\_POST, $\_COOKIE, $\_FILES, etc.):

```
$environment   = new Environment($_SERVER, fopen('php://input', 'w+'), $_POST, $_COOKIE, $_FILES);
$serverRequest = (new ServerRequestFactory())->create($environment);
```

#### Working with ServerRequest

[](#working-with-serverrequest)

##### Getting values

[](#getting-values)

ServerRequest comes with lot of handy methods for working with the request:

```
// Create ServerRequest from existing super global variables
$environment   = new Environment($_SERVER, fopen('php://input', 'w+'), $_POST, $_COOKIE, $_FILES);
$serverRequest = (new ServerRequestFactory())->create($environment);

// Return method of server request eg. GET
$serverRequest->getMethod();
// Return URI server request eg. http://foo.bar
$serverRequest->getUri();
// Return URI server query parameters eg. [ 'foo' => 'bar' ]
$serverRequest->getQueryParams();
// Get server parameters ($_SERVER)
$serverRequest->getServerParams();
// Get request cookies ($_COOKIE)
$serverRequest->getCookieParams();
// Returns an associative array of the message's headers
$serverRequest->getHeaders();
```

One of the most important part of the request its body. Request body can be obtained by calling method `getParsedBody`:

```
// Get parsed body of request
$serverRequest->getParsedBody();
```

Package parse raw body of request according the content type of request. Following content type are supported:

- json, if the content type is application/json,
- xml, if the content type is application/xml or text/xml (instance of SimpleXMLElement is returned) and
- query string, if the content type is application/x-www-form-urlencoded or multipart/form-data

Uploaded files are stored as tree of UploadedFile class instances. They can be obtained with method `getUploadedFiles`:

```
// Get request uploaded files as tree in following format:
//  => [ , ... ]
$serverRequest->getUploadedFiles();
```

##### Modifying request

[](#modifying-request)

Request can be modified by methods that are prefixed by *with* string. For example:

- withMethod() - change method of the request
- withQueryParams() - change query params
- withParsedBody() - change parsed body
- withCookieParams() - change cookie parameters

As the requests are immutable; all methods that change state retain the internal state of the current message and return an instance that contains the changed state.

```
$requestWithGet = $serverRequest->withMethod('GET');
$requestWithPost = $requestWithGet->withMethod('POST');

echo $requestWithGet; // print GET
echo $requestWithPost; // print POST
```

### Stream

[](#stream)

Stream provides a wrapper around the most common PHP streams operations, including serialization of the entire stream to a string.

Stream is usually used for describing of `Request` or `Response` body content.

New instance of Stream must be created from existing `Resource`:

```
// Create Stream from based on raw data from the request body.
$stream = new Stream(fopen('php://input', 'w+'));
```

Content of `Stream` can be easily converted to string with method `getContents`:

```
// Create Stream from based on raw data from the request body:
$stream = new Stream(fopen('php://input', 'w+'));
// Convert Stream into string:
echo $stream->getContents();
```

### Request

[](#request)

Request is representation of an outgoing, client-side request. It can for example represent request to some third party website.

Request object is created with method and URI as the parameters:

```
// Create request with GET method to the URI 'foo.bar':
$clientRequest = new Request('GET', 'foo.bar');
echo $clientRequest->getMethod(); // GET
```

Request object constructor also accept three additional optional parameters - headers, body and protocol.

Headers are represented by instance of `Headers` object, following snippet show creating request with header `X-Foo` set to value `Bar`:

```
// Prepare array with header X-Foo and value Bar:
$headers = ['X-Foo' => 'Bar'];
// Create request with GET method to the uri 'foo.bar' with header 'X-Foo: Bar':
$clientRequest = new Request('GET', 'foo.bar', $headers);
```

Request body can be string or instance of `Stream` class, if the string is provided an instance of `Stream` will be created from this string:

```
// Create request with GET method to the uri 'foo.bar' with body :
$clientRequest = new Request('GET', 'foo.bar', [], 'string body');
// Body is of type Stream it must be typecast to the string:
echo (string)$clientRequest->getBody(); // string body
```

Request is immutable; all methods that change state retain the internal state of the instance and return a new instance that contains the changed state.

*Note: package do not provide client for performing the actual request.*

### Response

[](#response)

Response is representation of an outgoing, server-side response. Usually represents data that will be send to a browser.

Constructor of response object has three optional parameters - status, headers and body. If the status is not provided status code 200 ('OK') is used. Default value for the rest of parameters is null.

```
// Create response with status 200, empty headers and body.
$response = new Response();
//
```

If you want to include additional headers you can do it in same way as in `Request`, by creating and setting instance of `Headers` class:

```
// Prepare array with header X-Foo and value Bar
$headers = ['X-Foo' => 'Bar'];
// Create response with status code 200 and header 'X-Foo: Bar'
$response = new Response(200, $headers);
```

Treatment of body is also same as in the case of `Request` class, it can be an instance of `Stream` or string.

```
// Create response with status code 200, empty headers and
$response = new Response(200, null, 'string body');
// Body is instance of Stream it must be typecast to the string.
echo (string)$response->getBody(); // string body
```

*Response is immutable; all methods that change state retain the internal state of the instance and return a new instance that contains the changed state.*

License
-------

[](#license)

The MIT License (MIT),

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~532 days

Total

2

Last Release

3032d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b87557270e483a60dafa29bf9c894b00b6e375439cae8f27091352b9cfcda173?d=identicon)[kambo](/maintainers/kambo)

---

Top Contributors

[![kambo-1st](https://avatars.githubusercontent.com/u/6493048?v=4)](https://github.com/kambo-1st "kambo-1st (56 commits)")[![jm42](https://avatars.githubusercontent.com/u/3297150?v=4)](https://github.com/jm42 "jm42 (2 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

httppsr-7httpmessage

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kambo-httpmessage/health.svg)

```
[![Health](https://phpackages.com/badges/kambo-httpmessage/health.svg)](https://phpackages.com/packages/kambo-httpmessage)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k1.0B3.2k](/packages/guzzlehttp-psr7)[psr/http-factory

PSR-17: Common interfaces for PSR-7 HTTP message factories

1.9k692.9M1.9k](/packages/psr-http-factory)[symfony/psr-http-message-bridge

PSR HTTP message bridge

1.3k296.6M807](/packages/symfony-psr-http-message-bridge)[php-http/message

HTTP Message related tools

1.3k263.9M678](/packages/php-http-message)[league/uri-interfaces

Common tools for parsing and resolving RFC3987/RFC3986 URI

538204.9M23](/packages/league-uri-interfaces)[laminas/laminas-diactoros

PSR HTTP Message implementations

546105.8M965](/packages/laminas-laminas-diactoros)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
