Setting a longer (or shorter) timeout for Laravel's Http client
Illuminate\Http\Client\ConnectionExceptionShort answer
The Laravel Http client waits 30 seconds for a response and 10 seconds to connect. A slower API throws a ConnectionException. Call timeout() with more seconds, and connectTimeout() if the connection itself is slow. Add retry() for flaky endpoints. Raise the limits only as far as the job or request that runs the call can wait.
Fix it fast
1Find out which cause you have
Print the exception message. It says whether the wait for the response or the connection step ran out of time.
try { Http::get('https://api.example.com/report'); } catch (\Illuminate\Http\Client\ConnectionException $e) { logger()->error($e->getMessage()); }2Match what you see, then jump to its fix
- 1
ConnectionException after about 30 seconds, while the same URL works in a browserThe response takes longer than the default 30 secondsFix - 2
The exception comes quickly (near 10 seconds) and no response was receivedThe connection step is the slow partFix - 3
The same request works when repeatedA flaky endpoint fails once and the code gives upFix
- 1
What does the error look like?
Illuminate\Http\Client\ConnectionExceptionRequest takes 30 seconds, then fails
Cause 1 of 3
The response takes longer than the default 30 seconds
1Check
By default the client times out after 30 seconds of waiting for the response. Measure how long the API needs.
$start = microtime(true);
Http::timeout(120)->get('https://api.example.com/report');
echo round(microtime(true) - $start, 1).' seconds';2Fix
Set a longer timeout for that request only, in seconds.
$response = Http::timeout(120)->get('https://api.example.com/report');3Verify
Run the call again and confirm it returns a response.
echo Http::timeout(120)->get('https://api.example.com/report')->status();Confirmed by laravel.com
Cause 2 of 3
The connection step is the slow part
1Check
connectTimeout is the time allowed to connect to the server, 10 seconds by default. A server that is down or unreachable fails here, and a longer timeout() does not help.
// a short connect limit shows quickly whether the connection step is the problem
Http::connectTimeout(2)->get('https://api.example.com/report');2Fix
If the connect time is genuinely slow, raise connectTimeout. If the host is unreachable, fix the network or DNS instead of waiting longer.
$response = Http::connectTimeout(20)->timeout(60)->get('https://api.example.com/report');3Verify
Repeat the request and check that a status code comes back.
echo Http::connectTimeout(20)->timeout(60)->get('https://api.example.com/report')->status();Confirmed by laravel.com
Cause 3 of 3
A flaky endpoint fails once and the code gives up
1Check
A single transient failure is thrown as a ConnectionException with no second attempt.
grep -rn "Http::" app/ | head2Fix
Use retry() with a count and a pause in milliseconds. The third argument limits retries to connection errors, so a 4xx from the API is not repeated.
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest;
use Throwable;
$response = Http::timeout(30)
->retry(3, 200, function (Throwable $e, PendingRequest $request) {
return $e instanceof ConnectionException;
})
->get('https://api.example.com/report');3Verify
Point the call at a URL that fails once, and check the response arrives on a later attempt.
echo $response->status();Confirmed by laravel.com
How do I stop it coming back?
- Set timeout() on every call to a third-party API, so one slow service cannot hold a request for 30 seconds.
- Move calls that need more than a few seconds into a queued job.
- Catch ConnectionException where the call is made and return a clear message or a fallback.
- Keep the timeout lower than the limit of whatever runs the code (web server, PHP, or queue worker).
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
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix