PHP code example of innmind / http-parser

1. Go to this page and download the library: Download innmind/http-parser 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/ */

    

innmind / http-parser example snippets


use Innmind\HttpParser\{
    Request\Parse,
    ServerRequest\Transform,
    ServerRequest\DecodeCookie,
    ServerRequest\DecodeQuery,
    ServerRequest\DecodeForm,
};
use Innmind\Time\Clock;
use Innmind\IO\IO;
use Innmind\Http\ServerRequest;
use Innmind\Immutable\Str;

// this data could come from anywhere
$raw = <<<RAW
POST /some-form HTTP/1.1
Host: innmind.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 23
Accept-Language: fr-fr
Cookie: PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1

some[key]=value&foo=bar

RAW;
$tmp = \fopen('php://temp', 'w+');
$io = IO::fromAmbientAuthority()
    ->streams()
    ->acquire($tmp);
$io
    ->write()
    ->sink(Sequence::of(Str::of($raw)))
    ->unwrap();
\fseek($tmp, 0);

$request = $parse($io->read())
    ->map(Transform::of())
    ->map(DecodeCookie::of())
    ->map(DecodeQuery::of())
    ->map(DecodeForm::of())
    ->match(
        static fn($request) => $request,
        static fn() => throw new \RuntimeException,
    );
$request instanceof ServerRequest, // true