Swoole\Http\Request->parse()

Declaration

<?php
Swoole\Http\Request->parse(string $data): int|false

Parameters

data

The raw data of a HTTP request packet as a string

Return

int|bool

The length of the parsed data or that the HTTP request is completed which will be true or false if not started yet

Description

Parse a HTTP request object with raw data. You can use this API to write your own HTTP request parser.

This allows you to parse your own request data, it will return the length of the packet which has been successfully resolved.

Since version 4.6.0+

Example

<?php
use Swoole\Http\Request;

$data = "GET /something?hello=world&test=2123 HTTP/1.1\r\n";
$data .= "Host: 127.0.0.1\r\n";
$data .= "Connection: keep-alive\r\n";
$data .= "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36\r\n";
$data .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\n";
$data .= "Accept-Encoding: gzip, deflate, br\r\n";

$request = Request::create(['parse_cookie' => false]);
var_dump($request);

var_dump($request->isCompleted());
var_dump($request->parse($data));

var_dump($request->parse("\r\n"));
var_dump($request->isCompleted());

var_dump($request);
var_dump($request->cookie);