This package provides an implementation to parse raw HTTP request and responses.
- PHP 7.4+
To install vircom/http-parser with Composer, run the following command:
$ composer require vircom/http-parser
You can see this library on Packagist.
Composer installs autoloader at ./vendor/autoloader.php
. If you use vircom/http-parser in your php script, add:
require_once 'vendor/autoload.php';
use VirCom\HttpParser\HttpParserFactory;
$request = "POST /cgi-bin/process.cgi HTTP/0.9\r\n"
. "User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\r\n"
. "\r\n"
. "exampleName1=exampleValue1&exampleName2=exampleValue2";
$parser = (new HttpParserFactory())->createRequestParser();
$result = $parser->parse($request);
<?php
require_once('vendor/autoload.php');
use VirCom\HttpParser\HttpParserFactory;
$response = "HTTP/1.1 200 OK\r\n"
. "Content-Type: application/json\r\n"
. "\r\n"
. "\r\n["
. " {\r\n"
. " \"id\": 10,\r\n"
. " \"name\": \"testName\",\r\n"
. " \"color\": \"testColor\"\r\n"
. " \"price\": \"testPrice\"\r\n"
. " }\r\n"
. "]";
$parser = (new HttpParserFactory())->createResponseParser();
$result = $parser->parse($response);
getStartLine()->getHttpMethod()
- returns HTTP request methodgetStartLine()->getTargetRequest()
- returns HTTP request target pathgetStartLine()->getHttpVersion()
- returns HTTP request protocol versiongetHeaders()
- returns HTTP request headers collectiongetHeaders()[n]->getName()
- returns HTTP request header namegetHeaders()[n]->getValues()
- returns HTTP request header valuesgetBody()
- returns HTTP request body content
getStartLine()->getHttpVersion()
- returns HTTP response protocol versiongetStartLine()->getStatusCode()
- returns HTTP response status codegetStartLine()->getStatusText()
- returns HTTP response status textgetHeaders()
- returns HTTP request headers collectiongetHeaders()[n]->getName()
- returns HTTP request header namegetHeaders()[n]->getValues()
- returns HTTP request header valuesgetBody()
- returns HTTP request body content
Bugs and feature request are tracked on GitHub
Monolog is licensed under the MIT License - see the LICENSE
file for details