PHP code example of slick / http

1. Go to this page and download the library: Download slick/http library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

slick / http example snippets


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;
    }
}

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;
    }
}

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();


use Slick\Http\Session;

$session = Session::create();

$session->set('foo', 'bar');

$session->read('foo', null); // if foo is not found the default (null) will be returned.

$session->erase('foo');
 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
#)
 php
public function sendRequest(RequestInterface $request): ResponseInterface;