2017 © Pedro Peláez
 

library http

Mutable HTTP model and HTTP client implementation

image

psx/http

Mutable HTTP model and HTTP client implementation

  • Sunday, March 18, 2018
  • by k42b3
  • Repository
  • 1 Watchers
  • 0 Stars
  • 22,164 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 12 Versions
  • 15 % Grown

The README.md

Http

About

This library contains elegant interfaces to describe HTTP message, middleware and client classes. It contains also corresponding reference implementations which can be used by every app which needs a solid HTTP stack., (*1)

HTTP

HTTP Body

HTTP Middleware

HTTP Client

Examples

Middleware

The following shows a simple middleware which always returns the response body Hello World!:, (*2)

<?php

use PSX\Http;
use PSX\Http\Server;

$chain = new Http\Filter\FilterChain();

// enforce user agent in HTTP request
$chain->on(new Http\Filter\UserAgentEnforcer());

// display maintenance file if available
$chain->on(new Http\Filter\Backstage(__DIR__ . '/.maintenance.html'));

// closure middleware
$chain->on(function(Http\RequestInterface $request, Http\ResponseInterface $response, Http\FilterChainInterface $filterChain){
    // get query parameter
    $request->getUri()->getParameter('foo');

    // set header
    $response->setHeader('X-Foo', 'bar');

    // write data to the body
    $response->getBody()->write('Hello World!');

    $filterChain->handle($request, $response);
});

// create global HTTP request and response
$request  = (new Server\RequestFactory())->createRequest();
$response = (new Server\ResponseFactory())->createResponse();

// start middleware chain
$chain->handle($request, $response);

// send response
(new Server\Sender())->send($response);

Client

The following sends an HTTP GET request to google:, (*3)

<?php

use PSX\Http\Client;
use PSX\Http\Exception\StatusCodeException;

// create HTTP client
$client = new Client\Client();

// build request
$request = new Client\GetRequest('https://google.com', ['Accept' => 'text/html']);

// send request
$response = $client->request($request);

// check response
if ($response->getStatusCode() == 200) {
    // get header
    $contentType = $response->getHeader('Content-Type');

    // output response body
    echo (string) $response->getBody();
} else {
    // the client never throws an exception for unsuccessful response codes but
    // you can do this explicit
    StatusCodeException::throwOnError($response);
}

Uploads

Example how to handle file uploads:, (*4)

<?php

use PSX\Http;
use PSX\Http\Server;

$chain = new Http\Filter\FilterChain();

// closure middleware
$chain->on(function(Http\RequestInterface $request, Http\ResponseInterface $response, Http\FilterChainInterface $filterChain){
    // get body
    $body = $request->getBody();

    if ($body instanceof Http\Stream\MultipartStream) {
        // move uploaded file to a new location
        $body->getPart('userfile')->move('/home/new/file.txt');

        // or access the file directly through the normal stream functions
        //$body->getPart('userfile')->read(32);

        // write data to the body
        $response->getBody()->write('Upload successful!');
    } else {
        // no upload so show form
        $html = <<<'HTML'



Send this file:
HTML; $response->getBody()->write($html); } $filterChain->handle($request, $response); }); // create global HTTP request and response $request = (new Server\RequestFactory())->createRequest(); $response = (new Server\ResponseFactory())->createResponse(); // start middleware chain $chain->handle($request, $response); // send response (new Server\Sender())->send($response);

The Versions

18/03 2018

dev-master

9999999-dev http://phpsx.org

Mutable HTTP model and HTTP client implementation

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

http

18/03 2018

v2.0.6

2.0.6.0 http://phpsx.org

Mutable HTTP model and HTTP client implementation

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

http

25/02 2018

v2.0.5

2.0.5.0 http://phpsx.org

Mutable HTTP model and HTTP client implementation

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

http

18/02 2018

v2.0.4

2.0.4.0 http://phpsx.org

Mutable HTTP model and HTTP client implementation

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

http

17/02 2018

v2.0.3

2.0.3.0 http://phpsx.org

Mutable HTTP model and HTTP client implementation

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

http

16/02 2018

v2.0.2

2.0.2.0 http://phpsx.org

Mutable HTTP model and HTTP client implementation

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

http

13/02 2018

v2.0.1

2.0.1.0 http://phpsx.org

Mutable HTTP model and HTTP client implementation

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

http

12/02 2018

v2.0.0

2.0.0.0 http://phpsx.org

Mutable HTTP model and HTTP client implementation

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

http

04/06 2017

v1.0.2

1.0.2.0 http://phpsx.org

Mutable HTTP model and HTTP client implementation

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

http

04/06 2017

v1.0.1

1.0.1.0 http://phpsx.org

Mutable HTTP model and HTTP client implementation

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

http

08/05 2016

v1.0.0

1.0.0.0 http://phpsx.org

Mutable HTTP model and HTTP client implementation

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

http

02/04 2016

v0.1.0

0.1.0.0 http://phpsx.org

Mutable HTTP model and HTTP client implementation

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

http