API Index — Namespaces, Packages, Reports, Indices
FastCgiClient
in package
FastCGI 1.0 RESPONDER client for ZealPHP's legacy-include dispatch path.
Implements the FCGI binary protocol (BEGIN_REQUEST → PARAMS → STDIN →
STDOUT + STDERR + END_REQUEST) over an OpenSwoole coroutine socket so the
call never blocks the event loop.
Protocol: https://fastcgi-devkit.org/doc/FCGI_Spec.html
Reference implementations: mod_proxy_fcgi.c, ngx_http_fastcgi_module.c
Table of Contents
Constants
- FCGI_ABORT_REQUEST : mixed = 2
- FCGI_BEGIN_REQUEST : mixed = 1
- FCGI_END_REQUEST : mixed = 3
- FCGI_GET_VALUES : mixed = 9
- FCGI_GET_VALUES_RESULT : mixed = 10
- FCGI_KEEP_CONN : mixed = 1
- FCGI_PARAMS : mixed = 4
- FCGI_RESPONDER : mixed = 1
- FCGI_STDERR : mixed = 7
- FCGI_STDIN : mixed = 5
- FCGI_STDOUT : mixed = 6
- FCGI_VERSION : mixed = 1
- HEADER_LEN : mixed = 8
- MAX_CONTENT : mixed = 65535
Properties
Methods
- __construct() : mixed
- encodeLength() : string
- Encode a single FCGI length field.
- encodeParams() : string
- Encode name-value pairs per FCGI 1.0 §3.4.
- encodeRecord() : string
- Build a complete FCGI record: 8-byte header + body + alignment padding.
-
parseStdout()
: array{status: int, headers: array
, body: string, stderr: string} - Parse raw
STDOUTbytes into status + headers + body. -
readResponse()
: array{status: int, headers: array
, body: string, stderr: string} - Read
STDOUT+STDERR+END_REQUESTrecords and assemble response. - recvExact() : string
- Read exactly
$nbytes from the coroutine socket, looping on short reads. -
request()
: array{status: int, headers: array
, body: string, stderr: string} - Dispatch one FCGI
RESPONDERrequest and return parsed response data. - connect() : Client
- sendAbort() : void
- Send
ABORT_REQUESTto the backend (client disconnect / error path). - sendBeginRequest() : void
- Send a
BEGIN_REQUESTrecord (role=RESPONDER,flags=0— no keep-conn). - sendParams() : void
- Encode and send
PARAMSrecords followed by emptyPARAMSterminator. - sendRaw() : void
- sendStdin() : void
- Send
STDINrecord(s) followed by emptySTDINterminator.
Constants
FCGI_ABORT_REQUEST
public
mixed
FCGI_ABORT_REQUEST
= 2
FCGI_BEGIN_REQUEST
public
mixed
FCGI_BEGIN_REQUEST
= 1
FCGI_END_REQUEST
public
mixed
FCGI_END_REQUEST
= 3
FCGI_GET_VALUES
public
mixed
FCGI_GET_VALUES
= 9
FCGI_GET_VALUES_RESULT
public
mixed
FCGI_GET_VALUES_RESULT
= 10
FCGI_KEEP_CONN
public
mixed
FCGI_KEEP_CONN
= 1
FCGI_PARAMS
public
mixed
FCGI_PARAMS
= 4
FCGI_RESPONDER
public
mixed
FCGI_RESPONDER
= 1
FCGI_STDERR
public
mixed
FCGI_STDERR
= 7
FCGI_STDIN
public
mixed
FCGI_STDIN
= 5
FCGI_STDOUT
public
mixed
FCGI_STDOUT
= 6
FCGI_VERSION
public
mixed
FCGI_VERSION
= 1
HEADER_LEN
public
mixed
HEADER_LEN
= 8
MAX_CONTENT
public
mixed
MAX_CONTENT
= 65535
Properties
$address
private
string
$address
$timeout
private
int
$timeout
Methods
__construct()
public
__construct([string $address = '127.0.0.1:9000' ][, int $timeout = 30 ]) : mixed
Parameters
- $address : string = '127.0.0.1:9000'
-
TCP
"host:port"or Unix"unix:/path/to/fpm.sock" - $timeout : int = 30
-
Socket timeout in seconds (
0= no timeout)
encodeLength()
Encode a single FCGI length field.
public
encodeLength(int $len) : string
≤ 127 → 1 byte; ≥ 128 → 4 bytes with MSB set.
Parameters
- $len : int
Return values
stringencodeParams()
Encode name-value pairs per FCGI 1.0 §3.4.
public
encodeParams(array<string, string> $params) : string
Length encoding (ngx_http_fastcgi_module.c:1092-1102, mod_proxy_fcgi.c:501-503):
len ≤ 127 → 1 byte
len ≥ 128 → 4 bytes with MSB set (big-endian, top bit = 1)
Parameters
- $params : array<string, string>
Return values
stringencodeRecord()
Build a complete FCGI record: 8-byte header + body + alignment padding.
public
encodeRecord(int $type, int $reqId, string $body) : string
Header layout (FCGI 1.0 §2.1):
version(1) type(1) requestIdB1(1) requestIdB0(1)
contentLengthB1(1) contentLengthB0(1) paddingLength(1) reserved(1)
Parameters
- $type : int
- $reqId : int
- $body : string
Return values
stringparseStdout()
Parse raw STDOUT bytes into status + headers + body.
public
parseStdout(string $stdout, string $stderr, int $appStatus) : array{status: int, headers: array, body: string, stderr: string}
CGI header block ends at first blank line (CRLFCRLF or LFLF).
Status: header is extracted and removed (nginx: ngx_http_fastcgi_module.c:648-656;
Apache: ap_scan_script_header_err_brigade_ex).
Parameters
- $stdout : string
- $stderr : string
- $appStatus : int
Return values
array{status: int, headers: arrayreadResponse()
Read STDOUT + STDERR + END_REQUEST records and assemble response.
public
readResponse(Client $conn, int $reqId) : array{status: int, headers: array, body: string, stderr: string}
Header state machine mirrors mod_proxy_fcgi.c:543-594 and
ngx_http_fastcgi_module.c:1690-1857.
Parameters
- $conn : Client
- $reqId : int
Return values
array{status: int, headers: arrayrecvExact()
Read exactly $n bytes from the coroutine socket, looping on short reads.
public
recvExact(Client $conn, int $n) : string
OpenSwoole sockets can return short reads; this loop ensures the full requested byte count is available before proceeding (partial-frame defence).
Parameters
- $conn : Client
- $n : int
Return values
stringrequest()
Dispatch one FCGI RESPONDER request and return parsed response data.
public
request(array<string, string> $params[, string $stdinBody = '' ]) : array{status: int, headers: array, body: string, stderr: string}
Parameters
- $params : array<string, string>
-
FCGI
PARAMS(CGI environment variables). - $stdinBody : string = ''
-
Request body (
POSTdata).
Tags
Return values
array{status: int, headers: arrayconnect()
private
connect() : Client
Return values
ClientsendAbort()
Send ABORT_REQUEST to the backend (client disconnect / error path).
private
sendAbort(Client $conn, int $reqId) : void
Parameters
- $conn : Client
- $reqId : int
sendBeginRequest()
Send a BEGIN_REQUEST record (role=RESPONDER, flags=0 — no keep-conn).
private
sendBeginRequest(Client $conn, int $reqId) : void
Body layout: roleB1(1) roleB0(1) flags(1) reserved×5
Source: mod_proxy_fcgi.c:321-339, FCGI spec §5.1
Parameters
- $conn : Client
- $reqId : int
sendParams()
Encode and send PARAMS records followed by empty PARAMS terminator.
private
sendParams(Client $conn, int $reqId, array<string, string> $params) : void
Multiple NV-pairs concatenate into one record body; fragments if total
exceeds MAX_CONTENT.
Source: mod_proxy_fcgi.c:466-525
Parameters
- $conn : Client
- $reqId : int
- $params : array<string, string>
sendRaw()
private
sendRaw(Client $conn, string $data) : void
Parameters
- $conn : Client
- $data : string
sendStdin()
Send STDIN record(s) followed by empty STDIN terminator.
private
sendStdin(Client $conn, int $reqId, string $body) : void
Source: mod_proxy_fcgi.c:742-758
Parameters
- $conn : Client
- $reqId : int
- $body : string