Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\Http\Request->parse(string $data): int|false
The raw data of a HTTP request packet as a string
The length of the parsed data or that the HTTP request is completed which will be true
or false
if not started yet
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+
<?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);