2017-25 © Pedro Peláez
 

library http-request-parser

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

image

toflar/http-request-parser

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

  • Thursday, June 14, 2018
  • by Toflar
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

HTTP Request Parser

, (*1)

THIS IS WORK IN PROGRESS WITHOUT ANY RELEASE, (*2)

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:, (*3)

<?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:, (*4)

<?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:, (*5)

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

The Versions

14/06 2018

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires