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

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

geggleto/http
=============

HTTP Component

2.0.0(10y ago)018MITPHPPHP &gt;=5.6.0

Since Aug 4Pushed 10y ago1 watchersCompare

[ Source](https://github.com/geggleto/http)[ Packagist](https://packagist.org/packages/geggleto/http)[ RSS](/packages/geggleto-http/feed)WikiDiscussions 2.0 Synced 1w ago

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

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

[](#http-component)

[![Build Status](https://camo.githubusercontent.com/85bf1ecbb1084554e00667e04abf83fb1a946ed7825cc9a4afe8aa3a5dc490ff/68747470733a2f2f7472617669732d63692e6f72672f676567676c65746f2f687474702e7376673f6272616e63683d322e30)](https://travis-ci.org/geggleto/http)

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

[](#installation)

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

```
geggleto/http ^2.0

```

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\Request;

$request = new Request($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER);
```

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->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 `Response` object is the data holder for the HTTP response. It has no constructor dependencies and can be instantiated with just:

```
use Http\Response;

$response =  new Response;
```

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)

```
