Skip to content

HttpClientFactory

Registry + factory for named HTTP clients, modelled on .NET’s services.AddHttpClient("name", c => ...) / IHttpClientFactory: you register a named client’s configuration once, then resolve it by name, and the same HttpClient instance is reused for that name for the lifetime of the process (a FrankenPHP worker keeps one per name) rather than being rebuilt on every call.

Registered as a container singleton (see Context::registerCoreServicesInContainer()), so app/plugin code can constructor-inject HttpClientFactory and pull named clients — and plugins contribute named-client configs via PluginRegistrar::httpClient().

final class HttpClientFactory

SourceHttp/Client/HttpClientFactory.php
ConstantValueDescription
DEFAULT'default'
MethodDescription
client(string $name = self::DEFAULT): HttpClientResolve a named client, building + memoizing it on first use.
configure(string $name, callable(HttpClientConfig): void $configurator): voidRegister (or overwrite) a named client’s configuration.
has(string $name): boolReports whether HttpClientFactory::client() can resolve this name.
reset(): voidEmpties the registry: every registered configurator, every memoized client and any default transport factory are discarded, leaving the factory as freshly constructed.
setDefaultTransportFactory(?callable $factory): voidOverride the transport used by clients that don’t set their own (default: TransportFactory::default()).

public function client(string $name = self::DEFAULT): HttpClient

Resolve a named client, building + memoizing it on first use.

ParameterTypeDescription
$namestring

Returns HttpClient

public function configure(string $name, callable(HttpClientConfig): void $configurator): void

Register (or overwrite) a named client’s configuration.

ParameterTypeDescription
$namestring
$configuratorcallable(HttpClientConfig): void

public function has(string $name): bool

Reports whether HttpClientFactory::client() can resolve this name.

True for any registered configurator, and always true for DEFAULT, which builds from an unconfigured HttpClientConfig when nobody registered it.

ParameterTypeDescription
$namestring

Returns bool

public function reset(): void

Empties the registry: every registered configurator, every memoized client and any default transport factory are discarded, leaving the factory as freshly constructed.

Clients already handed out keep working; they simply are no longer the ones this factory will return.

public function setDefaultTransportFactory(?callable $factory): void

Override the transport used by clients that don’t set their own (default: TransportFactory::default()).

ParameterTypeDescription
$factory?``callable