PHP SoapClient waits too long, or gives up too early
The SOAP call hangs for about a minute and then failsShort answer
SoapClient has two different clocks. The connection_timeout option limits only the connect step. Waiting for the service's reply is limited by the default_socket_timeout ini setting, 60 seconds by default. Set both on purpose, and make sure max_execution_time is not the one that cuts the call.
Fix it fast
1Find out which cause you have
Print the values that decide the wait, from the same PHP that runs the call (web and CLI can differ).
php -r 'var_dump(ini_get("default_socket_timeout"), ini_get("max_execution_time"));'2Match what you see, then jump to its fix
- 1
The connect is fast, then the call sits until the 60 second default expiresconnection_timeout is expected to limit the whole call, but it only limits connectingFix - 2
Long reports or exports fail after exactly the same number of seconds every timedefault_socket_timeout is shorter than the service needsFix - 3
The web request dies with a PHP maximum execution time error, not a SOAP errorThe script's own time limit ends the request firstFix - 4
Even fast calls are slow, and the access log of the WSDL host shows a hit per requestThe WSDL is downloaded on every requestFix
- 1
What does the error look like?
The SOAP call hangs for about a minute and then failsconnection_timeout set to 5 seconds, but a slow service still blocks the script
Cause 1 of 4
connection_timeout is expected to limit the whole call, but it only limits connecting
1Check
The manual says connection_timeout is a timeout in seconds for the connection to the service and does not define a timeout for services with slow responses.
grep -rn "connection_timeout" src/ app/ 2>/dev/null2Fix
Keep connection_timeout for the connect step, and lower default_socket_timeout for the reply. Set it right before building the client.
ini_set('default_socket_timeout', '15');
$client = new SoapClient($wsdl, [
'connection_timeout' => 5,
'exceptions' => true,
]);3Verify
Call a slow endpoint (or one that sleeps) and check that the call fails after about 15 seconds.
time php call-soap.phpConfirmed by php.net
Cause 2 of 4
default_socket_timeout is shorter than the service needs
1Check
default_socket_timeout is the timeout for socket-based streams, in seconds. The default is 60, and a negative value means no timeout.
php -r 'echo ini_get("default_socket_timeout"), PHP_EOL;'2Fix
Raise it for that call only, not globally, to a value slightly above the service's slowest normal answer.
ini_set('default_socket_timeout', '180');
$result = $client->GetReport($params);3Verify
The slow call now returns, and fast calls are not affected.
time php call-soap.phpConfirmed by php.net
Cause 3 of 4
The script's own time limit ends the request first
1Check
The PHP time limit defaults to 30 seconds for web requests and 0 for the CLI. On non-Windows systems with the default timer, time spent in stream operations is not counted toward it, so a long CPU-heavy parse of the response can still hit it.
php -r 'echo ini_get("max_execution_time"), PHP_EOL;'2Fix
Raise the limit for the one script or job that makes the call, and consider running long calls from the CLI or a queue worker.
set_time_limit(300);3Verify
The request now ends with the SOAP result instead of a time-limit fatal.
tail -n 20 /var/log/php8.3-fpm.logConfirmed by php.net
Cause 4 of 4
The WSDL is downloaded on every request
1Check
With a remote WSDL URL, the SOAP extension caches it on disk by default (soap.wsdl_cache_enabled=1, TTL 86400 seconds). Caching can be switched off by ini or by cache_wsdl.
php -r 'echo ini_get("soap.wsdl_cache_enabled"), " ", ini_get("soap.wsdl_cache_ttl"), PHP_EOL;'2Fix
Turn the cache back on for the client, or save the WSDL to a local file and use that path.
$client = new SoapClient($wsdl, [
'cache_wsdl' => WSDL_CACHE_DISK,
]);3Verify
The second request is clearly faster than the first.
time php call-soap.php && time php call-soap.phpConfirmed by php.net
How do I stop it coming back?
- Set both connection_timeout and default_socket_timeout in the code that builds the client, never rely on defaults.
- Log the call duration around each SOAP call so slow services show up before they time out.
- Run long SOAP calls from a queue worker, not from a web request.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
PHP stops the script after 30 seconds
Symfony\Component\ErrorHandler\Error\FatalError: Maximum execution time of 60 seconds exceeded
PHP ends a script that runs past max_execution_time (30 seconds by default for web requests).
5 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix