PHPackages                             tflori/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. tflori/http

Abandoned → [tflori/tal](/?search=tflori%2Ftal)Library[HTTP &amp; Networking](/categories/http)

tflori/http
===========

HTTP Component

v1.9.0(8y ago)1354MITPHPPHP &gt;=5.5.0

Since Aug 4Pushed 8y ago1 watchersCompare

[ Source](https://github.com/tflori/http)[ Packagist](https://packagist.org/packages/tflori/http)[ RSS](/packages/tflori-http/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (10)Dependencies (1)Versions (21)Used By (0)

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

[](#http-component)

[![Build Status](https://camo.githubusercontent.com/9c76ecfc38a51b63466571ed51bd152a7219da34ebf5d9e4c4dcd4785861f2c0/68747470733a2f2f7472617669732d63692e6f72672f74666c6f72692f687474702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/tflori/http)[![Coverage Status](https://camo.githubusercontent.com/73e3964c8fb6974814be9e53c5af1f577c7257ceaffcd4dd82527ba630ccef80/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f74666c6f72692f687474702f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/tflori/http?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/6dfabb9ad3db0c0f5a542051f9ddf71b991907b3eeeb758e7b9eab170728df70/68747470733a2f2f706f7365722e707567782e6f72672f74666c6f72692f687474702f762f737461626c652e737667)](https://packagist.org/packages/tflori/http)[![Total Downloads](https://camo.githubusercontent.com/b275de72f4bc8d3ae71ab3377eb5d1ccd8c98b6348703f2ecfad839e50881ea0/68747470733a2f2f706f7365722e707567782e6f72672f74666c6f72692f687474702f646f776e6c6f6164732e737667)](https://packagist.org/packages/tflori/http)[![License](https://camo.githubusercontent.com/8446a118389fd911a8cf1ad2c72e3f287e623c8b953ce8fab0fbd59ace8d9bb8/68747470733a2f2f706f7365722e707567782e6f72672f74666c6f72692f687474702f6c6963656e73652e737667)](https://packagist.org/packages/tflori/http)

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

[](#installation)

You can use composer to install this component:

```
composer require tflori/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'));
// equals to:
$request = HttpRequest::createFromGlobals();
```

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->getScheme();
$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 get the current data in the response:

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

To send the response use the following method:

```
$response->send();
```

> make sure not to send the response twice as you will get an error message.

### 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)

```
