PHPackages                             patricklouys/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. [HTTP &amp; Networking](/categories/http)
4. /
5. patricklouys/http

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

patricklouys/http
=================

HTTP Component

v1.4.0(9y ago)6626.3k—10%19[1 PRs](https://github.com/PatrickLouys/http/pulls)1MITPHPPHP &gt;=5.5.0CI failing

Since Aug 4Pushed 3y ago7 watchersCompare

[ Source](https://github.com/PatrickLouys/http)[ Packagist](https://packagist.org/packages/patricklouys/http)[ RSS](/packages/patricklouys-http/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (1)Versions (9)Used By (1)

Http Component
--------------

[](#http-component)

[![Build Status](https://camo.githubusercontent.com/c881c39ffa952631b13481a528d6982f77f14c8a8dd8405c1313ce52b047e87b/68747470733a2f2f7472617669732d63692e6f72672f5061747269636b4c6f7579732f687474702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/PatrickLouys/http) [![Coverage Status](https://camo.githubusercontent.com/9d2e00a1627ba46fee1348b5e41eb83a936b347b4bb18c6b757023ab8a6d4c8d/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f5061747269636b4c6f7579732f687474702f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/PatrickLouys/http?branch=master) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/b5187a19c11a9286aed26105597dc23bf9913ed804681ba906dc08b010b5379c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f5061747269636b4c6f7579732f687474702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/PatrickLouys/http/?branch=master) [![Latest Stable Version](https://camo.githubusercontent.com/953e1d62a23d562670e3f143bb6b1747ee6feb0739792442e2b9d597966f5150/68747470733a2f2f706f7365722e707567782e6f72672f7061747269636b6c6f7579732f687474702f762f737461626c652e737667)](https://packagist.org/packages/patricklouys/http) [![Total Downloads](https://camo.githubusercontent.com/18141b16df6feb02cd673f1b3fea531c4360f52c1ddfa32580a83b0c860c1ab2/68747470733a2f2f706f7365722e707567782e6f72672f7061747269636b6c6f7579732f687474702f646f776e6c6f6164732e737667)](https://packagist.org/packages/patricklouys/http) [![License](https://camo.githubusercontent.com/1cb9c851206c4501a7456c0b32ecc4e9e267ff160c237167d0946c7e6ed43843/68747470733a2f2f706f7365722e707567782e6f72672f7061747269636b6c6f7579732f687474702f6c6963656e73652e737667)](https://packagist.org/packages/patricklouys/http)

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

[](#installation)

You can use composer to install this component. The package is:

```
patricklouys/http

```

Basic Usage
-----------

[](#basic-usage)

### Request

[](#request)

The Request class provides an object oriented wrapper around the PHP superglobals. This makes it possible to inject it as a dependency into any of your classes that require it.

```
use Http\HttpRequest;

$request = new HttpRequest($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER, file_get_contents('php://input'));
```

Now you can use the following methods on the `$request` object:

```
$request->getParameter($key, $defaultValue = null);
$request->getFile($key, $defaultValue = null);
$request->getCookie($key, $defaultValue = null);
$request->getParameters();
$request->getQueryParameters();
$request->getBodyParameters();
$request->getRawBody();
$request->getCookies();
$request->getFiles();
$request->getMethod();
$request->getHttpAccept();
$request->getReferer();
$request->getUserAgent();
$request->getIpAddress();
$request->isSecure();
$request->getQueryString();
```

Please note that both GET and POST parameters are merged together and accessible with `getParameter`.

### Response

[](#response)

The `HttpResponse` object is the data holder for the HTTP response. It has no constructor dependencies and can be instantiated with just:

```
use Http\HttpResponse;

$response =  new HttpResponse;
```

The response can be modified with following methods:

```
$response->setStatusCode($statusCode, $statusText = null);
$response->addHeader($name, $value);
$response->setHeader($name, $value);
$response->addCookie(Cookie $cookie);
$response->deleteCookie(Cookie $cookie);
$response->setContent($content);
$response->redirect($url);
```

If you don't supply a status text with `setStatusCode` then an appropriate default status text will be selected for the HTTP status code if available.

`addHeader` adds a new header value without overwriting existing values, `setHeader` will overwrite an existing value.

The `redirect` method will set the status code and text for a 301 redirect.

`deleteCookie` will set the cookie content to nothing and put the expiration in the past.

The following two methods are available to send the response to the client:

```
$response->getHeaders();
$response->getContent();
```

They can be used like this:

```
foreach ($response->getHeaders() as $header) {
    header($header, false);
}

echo $response->getContent();
```

**The second parameter of `header` must be false. Otherwise existing headers will be overwritten.**

### Cookies

[](#cookies)

To avoid `new` calls in your classes and to have the ability to set default cookie settings for you application, there is a `CookieBuilder` class that you can use to create your cookie objects. It has the following methods available:

```
$cookieBuilder->setDefaultDomain($domain); // defaults to NULL
$cookieBuilder->setDefaultPath($path); // defaults to '/'
$cookieBuilder->setDefaultSecure($secure); // defaults to TRUE
$cookieBuilder->setDefaultHttpOnly($httpOnly); // defaults to TRUE
$cookieBuilder->build($name, $value); // returns the cookie object
```

You can use the following methods to manipulate an existing cookie:

```
$cookie->setValue($value);
$cookie->setMaxAge($seconds);
$cookie->setDomain($domain);
$cookie->setPath($path);
$cookie->setSecure($secure);
$cookie->setHttpOnly($httpOnly);
```

The cookie object can the be used with the `HttpResponse` methods `addCookie` and `deleteCookie`.

### Example

[](#example)

```
