2017 © Pedro Peláez
 

library http

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

image

slick/http

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

  • Wednesday, February 14, 2018
  • by fsilva
  • Repository
  • 2 Watchers
  • 0 Stars
  • 925 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 15 Versions
  • 1 % Grown

The README.md

Slick HTTP

Latest Version on Packagist ![Software License][ico-license] Quality Score ![Total Downloads][ico-downloads], (*1)

slick/http provides a complete implementation of PSR-7 (HTTP Message Interface) and PSR-18 (HTTP Client Interface), enabling developers to work seamlessly with HTTP messages and clients in a standardized way. By adhering to these PSR standards, the library ensures interoperability with other libraries and frameworks that follow the same specifications, promoting code reusability and consistency across projects. In addition, the library includes an HTTP server that functions as a middleware runner, allowing you to pass a collection of middleware objects to the server, which are called in sequence to compose a response for the current server request. This middleware approach provides a flexible, modular way to handle HTTP requests, enhancing the scalability and maintainability of your PHP applications., (*2)

This package is compliant with PSR-2 code standards and PSR-4 autoload standards. It also applies the semantic version 2.0.0 specification., (*3)

Install

Via Composer, (*4)

``` bash $ composer require slick/http, (*5)


## Usage ### Server Request Message This is an handy way to have a HTTP request message that has all necessary information that was sent by the client to the server. Its very simple to get this: ``` php use Slick\Http\Message\Server\Request; $request = new Request();

The $request encapsulates all data as it has arrived at the application from the CGI and/or PHP environment, including: - The values represented in $_SERVER. - Any cookies provided (generally via $_COOKIE) - Query string arguments (generally via $_GET, or as parsed via parse_str()) - Upload files, if any (as represented by $_FILES) - Deserialized body parameters (generally from $_POST), (*6)

Consider the following message: ``` txt POST /path?_goto=home HTTP/1.1 Host: www.example.org Content-Type: application/x-www-form-urlencoded; charset=utf-8 Content-Lenght: 5 Authorization: Bearer PAOIPOI-ASD9POKQWEL-KQWELKAD==, (*7)

foo=bar&bar=baz, (*8)

Now lest retrieve its information using the ``$request`` object:

``` php
echo $request->getHeaderLine('Authorization');  // will print "Bearer PAOIPOI-ASD9POKQWEL-KQWELKAD=="

print_r($request->getParsedBody());
# Array (
#   [foo] => bar,
#   [bar] => baz
#)

print_r($request->getQueryParam());
# Array (
#   [_goto] => home
#)

HTTP Server

The HTTP server is a middleware runner. You can pass a collection of middleware objects to the server and they all will be called in their sequence in order to compose a response for current server request., (*9)

Lets create a simple web application that will print out a greeting to a query parameter named 'name'. Lets first create our middleware classes:, (*10)

use Psr\Http\Server\MiddlewareInterface;
use Interop\Http\Server\RequestHandlerInterface;

class Greeting implements MiddlewareInterface
{

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler)
    {
        $params = $request->getQueryParams();
        $request = (isset($params['name'])
            ? $request->withAttribute('greeting', "Hello, {$params['name']}!")
            : $request;

        $response = $handler->handle($request);
        return $response;
    }
}

This middleware retrieves the query parameter with name and add an attribute to the request object passed to the next middleware in the stack. Lets create our printer:, (*11)

use Psr\Http\Server\MiddlewareInterface;
use Interop\Http\Server\RequestHandlerInterface;

class Printer implements MiddlewateInterface
{

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler)
    {
      $greeting = $request->getAttribute('greeting', false);
      $text = $greeting ?: 'Hi!';

      $response = $handler->handle($request);

      $response->getBody()->write($text);

      return $response;
    }
}

Now lets create our main server application:, (*12)

use Slick\Http\Server\MiddlewareStack;
use Slick\Http\Message\Response;
use Slick\Http\Message\Server\Request;

$stack = (new MiddlewareStack())
    ->push(new Greeting())
    ->push(new Printer())
    ->push(function () { return new Response(200); }); // Order matters!

$response = $stack->process(new Request);

// Emit headers iteratively:
foreach ($response->getHeaders() as $name => $values) {
    header(sprintf('%s: %s', $name, implode(', ', $value)), false);
}

// Print out the message body
echo $response->getBody();

HTTP Client

Working with HTTP requests (as a client) is one of the most common tasks nowadays. Almost every application need to retrieve data from a web service or API and therefore an HTTP client is a must have., (*13)

Slick\Http\Client\CurlHttpClient implments PSR-18 HTTP Client, (*14)

``` php public function sendRequest(RequestInterface $request): ResponseInterface;, (*15)


It depends on PHP's ``cURL`` extension to connect and make HTTP requests. Lets see an example: ``` php use Psr\Http\Message\ResponseInterface; use Slick\Http\Client\CurlHttpClient; use Slick\Http\Message\Request; use Slick\Http\Message\Uri; $request = new Request('GET', new Uri('https://example.com')); $client = new CurlHttpClient(); $response = $client->sendRequest($request); $data = json_decode($response->getBody()->getContents());

Session

Slick\Http\Session has a very simple and ease implementation. By default it will use the PHP session handling and it comes with a simple factory that you can use to create it:, (*16)

use Slick\Http\Session;

$session = Session::create();
Write session data
$session->set('foo', 'bar');
Read session data
$session->read('foo', null); // if foo is not found the default (null) will be returned.
Deleting session data
$session->erase('foo');

You can create your own session drivers if you need to encrypt you data or change the save handler to save in a database by simply implementing the Slick\Http\Session\SessionDriverInterface. It also comes with a Slick\Http\Session\Driver\AbstractDriver class that has all the basic operations of the interface already implemented., (*17)

Please check documentation site for a complete reference., (*18)

Change log

Please see CHANGELOG for more information on what has changed recently., (*19)

Testing

bash $ composer test, (*20)

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details., (*21)

Security

If you discover any security related issues, please email slick.framework@gmail.com instead of using the issue tracker., (*22)

Credits

License

The MIT License (MIT). Please see License File for more information., (*23)

The Versions

14/02 2018

dev-master

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

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 http request psr-15 slick

14/02 2018

dev-develop

dev-develop https://github.com/slickframework/http

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 http request psr-15 slick

14/02 2018

v2.0.0-RC7

2.0.0.0-RC7 https://github.com/slickframework/http

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 http request psr-15 slick

05/02 2018

v2.0.0-RC6

2.0.0.0-RC6 https://github.com/slickframework/http

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 http request psr-15 slick

13/12 2017

v2.0.0-RC5

2.0.0.0-RC5 https://github.com/slickframework/http

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 http request psr-15 slick

13/12 2017

v2.0.0-RC4

2.0.0.0-RC4 https://github.com/slickframework/http

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 http request psr-15 slick

13/12 2017

v2.0.0-RC3

2.0.0.0-RC3 https://github.com/slickframework/http

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 http request psr-15 slick

06/12 2017

v2.0.0-RC2

2.0.0.0-RC2 https://github.com/slickframework/http

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 http request psr-15 slick

05/11 2017

v2.0.0-RC1

2.0.0.0-RC1 https://github.com/slickframework/http

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 http request psr-15 slick

27/10 2017

dev-feature/2.0

dev-feature/2.0 https://github.com/slickframework/http

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

http request slick

23/06 2016

dev-support/v1.2.x

dev-support/v1.2.x https://github.com/slickframework/http

`Slick/Http` is an useful library for Web application foundation. It implements the PSR-7 message interface and has middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

http request slick

23/06 2016

v1.2.3

1.2.3.0 https://github.com/slickframework/http

`Slick/Http` is an useful library for Web application foundation. It implements the PSR-7 message interface and has middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

http request slick

22/02 2016

v1.2.2

1.2.2.0 https://github.com/slickframework/http

`Slick/Http` is an useful library for Web application foundation. It implements the PSR-7 message interface and has middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

http request slick

22/02 2016

v1.2.1

1.2.1.0 https://github.com/slickframework/http

`Slick/Http` is an useful library for Web application foundation. It implements the PSR-7 message interface and has middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

http request slick

30/01 2016

v1.2.0

1.2.0.0 https://github.com/slickframework/http

`Slick/Http` is an useful library for Web application foundation. It implements the PSR-7 message interface and has middleware server, session handling and an HTTP client.

  Sources   Download

MIT

The Requires

 

The Development Requires

http request slick