Alpha ZealPHP is early-stage and under active development. APIs may change between minor versions until v1.0. Feedback and bug reports welcome on GitHub.
API Index — Namespaces, Packages, Reports, Indices

FastCgiClient
in package

FinalYes

FastCGI 1.0 RESPONDER client for ZealPHP's legacy-include dispatch path.

Implements the FCGI binary protocol (BEGIN_REQUESTPARAMSSTDINSTDOUT + 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

$address  : string
$timeout  : int

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 STDOUT bytes into status + headers + body.
readResponse()  : array{status: int, headers: array, body: string, stderr: string}
Read STDOUT + STDERR + END_REQUEST records and assemble response.
recvExact()  : string
Read exactly $n bytes from the coroutine socket, looping on short reads.
request()  : array{status: int, headers: array, body: string, stderr: string}
Dispatch one FCGI RESPONDER request and return parsed response data.
connect()  : Client
sendAbort()  : void
Send ABORT_REQUEST to the backend (client disconnect / error path).
sendBeginRequest()  : void
Send a BEGIN_REQUEST record (role=RESPONDER, flags=0 — no keep-conn).
sendParams()  : void
Encode and send PARAMS records followed by empty PARAMS terminator.
sendRaw()  : void
sendStdin()  : void
Send STDIN record(s) followed by empty STDIN terminator.

Constants

FCGI_GET_VALUES_RESULT

public mixed FCGI_GET_VALUES_RESULT = 10

Properties

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
string

encodeParams()

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
string

encodeRecord()

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
string

parseStdout()

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: array, body: string, stderr: string}

readResponse()

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: array, body: string, stderr: string}

recvExact()

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
string

request()

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 (POST data).

Tags
throws
FastCgiException

on protocol error, timeout, or connection failure.

Return values
array{status: int, headers: array, body: string, stderr: string}

sendAbort()

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
On this page