Laminas Http Client setTimeout versus Curl connect and read timeouts
0 reputation · 30 Mar 2025, 21:51 UTC
0 reputation · 30 Mar 2025, 21:51 UTC
Configure outbound HTTP requests in Laminas Http 2.x or 3.x so that connection establishment and response reading have separate time limits, while keeping the client code portable across adapters.
Laminas\Http\Client::setTimeout() is documented as a client-level timeout, but adapter documentation indicates it is interpreted per adapter and may cover connect, read, or both without central validation. The Curl adapter exposes setCurlOptions() for CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT, which gives separate control but couples the caller to the Curl adapter. No cooperative cancellation token is documented; stopping an in-flight request relies on timeout expiration or adapter destruction.
setTimeout() enforce distinct connect and read limits across Curl and Socket adapters, or only a combined limit?setCurlOptions() on the Curl adapter the only documented path to separate connect and read timeouts?Laminas\\Http\\Client::setTimeout() does not enforce distinct connection and read limits across adapters; it provides a single, unified timeout value that adapters interpret according to their own internal logic. Because there is no shared specification for "connect" vs "read" timeouts in the base client API, this method cannot be used to set separate limits portably.
The behavior of setTimeout() varies by the active adapter:
CURLOPT_TIMEOUT, which limits the entire transfer duration (connection + data transfer).stream_set_timeout, which primarily functions as a read timeout.Using setCurlOptions() on the Curl adapter is the only documented way to separate these limits. This approach couples your code to the Curl adapter and will fail or be ignored if the adapter is switched to Socket.
// Example: Separate limits using the Curl adapter
$client = new \\Laminas\\Http\\Client();
$client->setAdapter(new \\Laminas\\Http\\Client\\Adapter\\Curl());
// Set connection timeout to 2s and total request timeout to 30s
$client->getAdapter()->setCurlOptions([
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_TIMEOUT => 30,
]);
There is no supported mechanism to cancel an in-flight request via a cancellation token or interrupt signal without destroying the adapter instance or allowing the timeout to expire. Once send() is called, the execution is synchronous and blocking until the adapter returns a response or hits a timeout.
To verify your timeout configuration, you can test against a non-routable IP address (e.g., 10.255.255.1):
CURLOPT_CONNECTTIMEOUT to 1 second and CURLOPT_TIMEOUT to 30 seconds.setCurlOptions is no longer effective, confirming the lack of portability.Diagnostic Note: Are you running this in a CLI environment or via a web server? If the latter, max_execution_time in php.ini may trigger before the client timeout expires, masking the adapter's behavior.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.