2017 © Pedro Peláez
 

library http

HTTP Component

image

tflori/http

HTTP Component

  • Monday, July 2, 2018
  • by tflori
  • Repository
  • 1 Watchers
  • 1 Stars
  • 343 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 12 Forks
  • 0 Open issues
  • 21 Versions
  • 27 % Grown

The README.md

Http Component

Build Status Coverage Status Latest Stable Version Total Downloads License, (*1)

Installation

You can use composer to install this component:, (*2)

composer require tflori/http

Basic Usage

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., (*3)

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:, (*4)

$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., (*5)

Response

The HttpResponse object is the data holder for the HTTP response. It has no constructor dependencies and can be instantiated with just:, (*6)

use Http\HttpResponse;

$response =  new HttpResponse;

The response can be modified with following methods:, (*7)

$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., (*8)

addHeader adds a new header value without overwriting existing values, setHeader will overwrite an existing value., (*9)

The redirect method will set the status code and text for a 301 redirect., (*10)

deleteCookie will set the cookie content to nothing and put the expiration in the past., (*11)

The following two methods are available to get the current data in the response:, (*12)

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

To send the response use the following method:, (*13)

$response->send();

make sure not to send the response twice as you will get an error message., (*14)

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:, (*15)

$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:, (*16)

$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., (*17)

Example

<?php

use Http\HttpRequest;
use Http\HttpResponse;
use Http\CookieBuilder;

$loader = require_once __DIR__ . '/vendor/autoload.php';

$cookieBuilder = new CookieBuilder();

// Disable the secure flag because this is only an example
$cookieBuilder->setDefaultSecure(false);

$request = HttpRequest::createFromGlobals();
$response = new HttpResponse();

$content = '

Hello World

'; $content .= $request->getCookie('TestCookie', 'Cookie is not set.'); if ($request->getParameter('setCookie') === 'true') { $cookie = $cookieBuilder->build('TestCookie', 'Cookie is set.'); $response->addCookie($cookie); } $response->setContent($content); $response->send();

The Versions

02/07 2018

dev-master

9999999-dev

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

02/07 2018

v1.9.0

1.9.0.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

08/08 2017

v1.8.0

1.8.0.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

18/01 2017

v1.7.4

1.7.4.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

18/01 2017

v1.7.3

1.7.3.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

22/11 2016

v1.7.2

1.7.2.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

21/11 2016

v1.7.1

1.7.1.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

21/11 2016

v1.7.0

1.7.0.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

21/11 2016

v1.6.2

1.6.2.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

21/11 2016

v1.6.1

1.6.1.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

21/11 2016

dev-feature-relativePath

dev-feature-relativePath

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

20/11 2016

v1.6.0

1.6.0.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

07/11 2016

v1.5.0

1.5.0.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys
by Thomas Flori

http request response

18/08 2016

v1.4.0

1.4.0.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys

http request response

10/11 2015

v1.3.0

1.3.0.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys

http request response

01/02 2015

v1.2.0

1.2.0.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys

http request response

21/01 2015

dev-2.0-dev

dev-2.0-dev

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys

http request response

17/09 2014

v1.1.0

1.1.0.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys

http request response

10/09 2014

v1.0.2

1.0.2.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys

http request response

19/08 2014

v1.0.1

1.0.1.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys

http request response

04/08 2014

v1.0.0

1.0.0.0

HTTP Component

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Patrick Louys

http request response