2017 © Pedro Peláez
 

library http

HTTP abstraction library, extracted dependency of fracture/router.

image

fracture/http

HTTP abstraction library, extracted dependency of fracture/router.

  • Thursday, March 10, 2016
  • by teresko
  • Repository
  • 2 Watchers
  • 5 Stars
  • 141 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 9 Versions
  • 1 % Grown

The README.md

Fracture\Http

Build Status Code Coverage Scrutinizer Packagist, (*1)

Introduction

A simple abstraction for handling the HTTP requests and responses. Library is made for interacting with fracture\routing and provides simple object-oriented interface., (*2)

Installation

You can add the library to your project using composer with following command:, (*3)

composer require fracture/http

Usage

All of the following code will assume that the Composer's autoloader has already been included., (*4)

Basic request initialization

While initializing a new Request instance manually is possible, for said instance to be fully prepared, it require several additional steps. To simplify this process, you should use RequestBuider, which will perform all of those steps:, (*5)

<?php
// -- unimportant code above --

$builder = new Fracture\Http\RequestBuilder;
$request = $builder->create([
    'get'    => $_GET,
    'files'  => $_FILES,
    'server' => $_SERVER,
    'post'   => $_POST,
    'cookies'=> $_COOKIE,
]);

Use of this code fragment is sufficient for any basic website and will produces a ready-to-use Request instance., (*6)

Requests and REST

When creating a site, that provides REST API, a common practice is to implement API versioning via HTTP Accept and Content-Type headers. But, if you send a POST query with custom Content-Type header, PHP will not populate $_POST with the information, that you sent to the server. And there are no $_PUT and $_DETETE superglobals in PHP., (*7)

To retrieve data information in a usable form, you have define a content parser, which, if the media type matches, is executed to supplement the Request instance with additional parameters., (*8)

<?php
// -- unimportant code above --

$builder = new Http\RequestBuilder;
$builder->addContentParser('application/json', function () {
    $data =  json_decode(file_get_contents('php://input'), true);

    if ($data === null) {
        $data = [];
    }

    return $data;
});
$request = $builder->create([
    'server' => $_SERVER,
    'cookies'=> $_COOKIE,
]);

It is possible for RequestBuilder instance to have multiple content parsers added., (*9)

Content parsers

A content parser is defined as an anonymous function, which will be executed with Fracture\Http\Headers\ContentType and Fracture\Http\Request instances as parameters and is expected to return an array of name => value pairs for parameters., (*10)

array function([ Fracture\Http\Headers\ContentType $header [, Fracture\Http\Request $request]])

You can also use content parsers to override Request attributes. For example, if you want to alter the request method, when submitting for with "magic" parameter like <input type="hidden" name="_my_method" value="PUT" /> (which is a common approach for making more RESTful and bypass the limitations of standard webpage):, (*11)

<?php
// -- unimportant code above --

$builder->addContentParser('*/*', function ($header, $request) {
    $override = $request->getParameter('_my_method');
    if ($override) {
        $request->setMethod($override);
    }
    return [];
});

Accessing data in the request

When the instance of Request has been fully initialized (preferably, using RequestBuilder), you gain ability to extract several types of information from this abstraction., (*12)

Parameters

To retrieve a parameter from an initialized Request instance you have to use getParameter() method:, (*13)

<?php
// -- unimportant code above --

$id = $request->getParameter('id');

When instance has been produced using RequestBuilder, it will contain parameter values from $_GET, $_POST and the data from content parsers., (*14)

Important!
If your code contains two parameters in $_GET and $_POST with same name, it will trigger a warning. Same warning will also be triggered, if one of the content parsers returned parameter, which already existed in $_GET or $_POST. The library is designed with an assumption, that having such overlap is an unintentional mistake., (*15)

The getParameter() method has the following signature:, (*16)

mixed Request::getParameter( string $name );

If parameter with the provided name does not exist, the method will return null., (*17)

Cookies

Retrieval of cookies is done using getCookies() method., (*18)

<?php
// -- unimportant code above --

$token = $request->getCookie('token');

The getCookie() method has the following signature:, (*19)

mixed Request::getCookie( string $name );

If parameter with the provided name does not exist, the method will return null., (*20)

File uploads

The method for retrieving uploaded files from Request instance is getUpload():, (*21)

<?php
// -- unimportant code above --

$file = $request->getUpload('attachment');

This method is called with the input's name as parameter. Depending on the structure of your upload form, it will return either an instance of UploadedFile, if your HTML had name="attachment", or FileBag instance, if form element had name="attachment[]"., (*22)

If no file has been uploaded with the given field name, then the method will return null., (*23)

The method signature is:, (*24)

mixed Request::getUpload( string $name );

Request method

To retrieve the HTTP request method, you have to use getMethod() method., (*25)

<?php
// -- unimportant code above --

$method = $request->getMethod();

It has the following signature:, (*26)

string Request::getMethod( void );

Headers

Currently the Request instance lets you access abstractions for Accept and Content-Type HTTP headers. It can be done using getAcceptHeader() and getContentTypeHeader() methods. These methods have the following signatures:, (*27)

Fracture\Http\Headers\Accept Request::getAcceptHeader( void )

and, (*28)

Fracture\Http\Headers\ContentType Request::getContentTypeHeader( void )

The Versions

10/03 2016

dev-master

9999999-dev https://github.com/fracture/http

HTTP abstraction library, extracted dependency of fracture/router.

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

The Development Requires

by Mārtiņš Tereško

10/03 2016

0.4.2

0.4.2.0 https://github.com/fracture/http

HTTP abstraction library, extracted dependency of fracture/router.

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

The Development Requires

by Mārtiņš Tereško

07/03 2016

0.4.1

0.4.1.0 https://github.com/fracture/http

HTTP abstraction library, extracted dependency of fracture/router.

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

The Development Requires

by Mārtiņš Tereško

06/03 2016

0.4.0

0.4.0.0 https://github.com/fracture/http

HTTP abstraction library, extracted dependency of fracture/router.

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

The Development Requires

by Mārtiņš Tereško

22/02 2016

0.3.2

0.3.2.0 https://github.com/fracture/http

HTTP abstraction library, extracted dependency of fracture/router.

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

The Development Requires

by Mārtiņš Tereško

22/02 2016

0.3.1

0.3.1.0 https://github.com/fracture/http

HTTP abstraction library, extracted dependency of fracture/router.

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

The Development Requires

by Mārtiņš Tereško

13/02 2016

0.3.0

0.3.0.0 https://github.com/fracture/http

HTTP abstraction library from Fracture PHP framework for use with composer

  Sources   Download

BSD

The Requires

  • php >=5.4.0

 

The Development Requires

by Mārtiņš Tereško

20/03 2014

0.2.0

0.2.0.0 https://github.com/fracture/http

HTTP abstraction library from Fracture PHP framework for use with composer

  Sources   Download

BSD

The Requires

 

The Development Requires

by Mārtiņš Tereško

19/03 2014

0.1.0

0.1.0.0 https://github.com/fracture/http

HTTP abstraction library from Fracture PHP framework for use with composer

  Sources   Download

BSD

The Requires

 

The Development Requires

by Mārtiņš Tereško