vircom / http-parser
HTTP requests and responses parser library
1.0.1
2020-05-05 05:40 UTC
Requires
- php: ^7.4
- myclabs/php-enum: ^1.7
Requires (Dev)
- php-coveralls/php-coveralls: ^2.2
- phpstan/phpstan: ^0.12.19
- phpunit/phpunit: ^9.1
- symplify/easy-coding-standard: ^7.2
- vimeo/psalm: ^3.11
This package is auto-updated.
Last update: 2024-10-05 15:26:36 UTC
README
This package provides an implementation to parse raw HTTP request and responses.
Prerequisites
- PHP 7.4+
Installation
Install by composer
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';
Usage:
Parsing request
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);
Parsing response
<?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);
Documentation
Request
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
Response
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
About
Submitting bugs and feature requests
Bugs and feature request are tracked on GitHub
License
Monolog is licensed under the MIT License - see the LICENSE
file for details