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

HTTP
in package

FinalYes

Ergonomic outbound HTTP wrapper around OpenSwoole\Coroutine\Http\Client.

Yields naturally under Runtime::HOOK_ALL (the default in coroutine mode). Outside a coroutine context, calls wrap themselves in Coroutine::run() so sync callers (php -r, unit tests) also work.

For the common case — JSON request/response, short timeout, a handful of headers — this is one call:

$r = HTTP::get('https://api.example.com/users', ['Authorization' => 'Bearer ...']);
if ($r->ok()) { return $r->json(); }

$r = HTTP::post('https://hooks.slack.com/...', body: ['text' => 'hi']);

For concurrent fan-out (N requests in parallel), use HTTP::all() — built on App::parallel().

For complex multipart uploads / streaming bodies, drop down to OpenSwoole\Coroutine\Http\Client directly — this wrapper is deliberately small.

Table of Contents

Methods

all()  : array<int, HTTPResponse>
Concurrent fan-out. Each entry in $requests is a callable that returns an HTTPResponse — typically a thunk like fn() => HTTP::get('...'). Results in input order. Uses App::parallel() under the hood — every request gets its own coroutine.
delete()  : HTTPResponse
get()  : HTTPResponse
post()  : HTTPResponse
POST with optional body. When $body is null, sends an empty body.
put()  : HTTPResponse
request()  : HTTPResponse
Send a request. Arbitrary method. Network errors (DNS, connect, TLS, timeout) come back as an HTTPResponse with status=0 + error=<Throwable> rather than throwing — handlers can check $r->failed() once at the top of the response-processing block instead of try-catching every call.
flattenHeaders()  : array<int, string>
Flatten an associative header map to curl's ['Name: value', ...] form.
hasHeaderCi()  : bool
Case-insensitive header check — outbound headers are user-provided.
requestBlocking()  : HTTPResponse
Blocking transport used when no coroutine scheduler is available — a reactor worker in mixed / legacy-cgi mode, where the coroutine HTTP client can't run and nesting Coroutine::run() deadlocks (#429). Uses ext-curl, which blocks the worker for the request duration; that is the correct, mod_php-equivalent behaviour in those sequential-per-worker modes. Returns the same HTTPResponse shape as the coroutine path.

Methods

all()

Concurrent fan-out. Each entry in $requests is a callable that returns an HTTPResponse — typically a thunk like fn() => HTTP::get('...'). Results in input order. Uses App::parallel() under the hood — every request gets its own coroutine.

public static all(array<int, callable(): HTTPResponse$requests) : array<int, HTTPResponse>
Parameters
$requests : array<int, callable(): HTTPResponse>
Return values
array<int, HTTPResponse>

delete()

public static delete(string $url[, array<string, string> $headers = [] ][, float $timeout = 30.0 ]) : HTTPResponse
Parameters
$url : string
$headers : array<string, string> = []
$timeout : float = 30.0
Return values
HTTPResponse

get()

public static get(string $url[, array<string, string> $headers = [] ][, float $timeout = 30.0 ]) : HTTPResponse
Parameters
$url : string
$headers : array<string, string> = []
$timeout : float = 30.0
Return values
HTTPResponse

post()

POST with optional body. When $body is null, sends an empty body.

public static post(string $url[, mixed $body = null ][, array<string, string> $headers = [] ][, float $timeout = 30.0 ]) : HTTPResponse

When $body is array, JSON-encodes + sets Content-Type: application/json (unless a Content-Type header is already set). When $body is string, sends as-is.

Parameters
$url : string
$body : mixed = null
$headers : array<string, string> = []
$timeout : float = 30.0
Return values
HTTPResponse

put()

public static put(string $url[, mixed $body = null ][, array<string, string> $headers = [] ][, float $timeout = 30.0 ]) : HTTPResponse
Parameters
$url : string
$body : mixed = null
$headers : array<string, string> = []
$timeout : float = 30.0
Return values
HTTPResponse

request()

Send a request. Arbitrary method. Network errors (DNS, connect, TLS, timeout) come back as an HTTPResponse with status=0 + error=<Throwable> rather than throwing — handlers can check $r->failed() once at the top of the response-processing block instead of try-catching every call.

public static request(string $method, string $url[, mixed $body = null ][, array<string, string> $headers = [] ][, float $timeout = 30.0 ]) : HTTPResponse
Parameters
$method : string
$url : string
$body : mixed = null
$headers : array<string, string> = []
$timeout : float = 30.0
Return values
HTTPResponse

flattenHeaders()

Flatten an associative header map to curl's ['Name: value', ...] form.

private static flattenHeaders(array<string, string> $headers) : array<int, string>
Parameters
$headers : array<string, string>
Return values
array<int, string>

hasHeaderCi()

Case-insensitive header check — outbound headers are user-provided.

private static hasHeaderCi(array<string, string> $headers, string $name) : bool
Parameters
$headers : array<string, string>
$name : string
Return values
bool

requestBlocking()

Blocking transport used when no coroutine scheduler is available — a reactor worker in mixed / legacy-cgi mode, where the coroutine HTTP client can't run and nesting Coroutine::run() deadlocks (#429). Uses ext-curl, which blocks the worker for the request duration; that is the correct, mod_php-equivalent behaviour in those sequential-per-worker modes. Returns the same HTTPResponse shape as the coroutine path.

private static requestBlocking(string $method, string $url, string $bodyStr, array<string, string> $headers, float $timeout) : HTTPResponse
Parameters
$method : string
$url : string
$bodyStr : string
$headers : array<string, string>
$timeout : float
Tags
infection-ignore-all

Integration-only: this path is reached solely from a live reactor worker (mixed / legacy-cgi, App::$server set) making a real outbound curl request — it cannot be exercised under the per-mutant Unit harness (no server, no live endpoint), so every mutant here is uncovered. Matches infection.json5's documented exclusion of integration-only code from the MSI baseline. Behaviour is covered by the #429 integration scenario, not Unit.

Return values
HTTPResponse
On this page