toflar/http-request-parser

Parses the string representation of an HTTP request into a PHP superglobal like array

dev-master 2018-06-14 11:50 UTC

This package is auto-updated.

Last update: 2024-03-28 03:35:22 UTC


README

THIS IS WORK IN PROGRESS WITHOUT ANY RELEASE

This tiny library parses an HTTP request from its raw string representation to PHP superglobal like arrays. Typical PHP libraries such as the Symfony HttpFoundation provide ways to represent the current request as an object by using the PHP globals like so:

<?php

use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();

However, if you have a string representation of a request, many of them do not support parsing these. This library parses the raw HTTP request and provides access to all the superglobals as PHP would create them. It's easiest to understand by using an example:

<?php

use Toflar\HttpRequestParser\Parser;

$raw = <<<REQUEST
GET /foobar?test=foo%20bar HTTP/1.1
Accept: application/json
Host: example.com
Connection: close
REQUEST;

$parser = new Parser($raw);

var_dump($parser->getGet()); // would output the equivalent of $_GET (decoded as PHP would)

You can use the results to create e.g. Symfony requests from these values then:

<?php

use Symfony\Component\HttpFoundation\Request;
use Toflar\HttpRequestParser\Parser;

$raw = <<<REQUEST
GET /foobar?test=foo%20bar HTTP/1.1
Accept: application/json
Host: example.com
Connection: close
REQUEST;

$parser = new Parser($raw);

$request = new Request(
    $parser->getGet(),
    $parser->getServer(),
    [],
    $parser->getCookie(),
    $parser->getFiles(),
    $parser->getServer(),
    $parser->getBody()
);