Guzzle or Http::get fails with cURL error 6 in Laravel
cURL error 6: Could not resolve host: api.example.com (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)Short answer
cURL error 6 means the machine running PHP could not turn the host name in the URL into an IP address. The request never left your server. Print the exact URL being called first. It is usually empty or wrong because env() returned null, or the container has no working DNS.
Fix it fast
1Find out which cause you have
Turn on Guzzle's debug output for one call. It prints the exact host libcurl tried to look up, which shows at once whether the URL is wrong or the name server is.
php artisan tinker >>> Http::withOptions(['debug' => true])->get('https://api.example.com/health');2Match what you see, then jump to its fix
- 1
The debug output shows a host like an empty string, 'http:' or a name with no domain partThe host in the URL is empty or wrong because env() returns nullFix - 2
The URL is correct and it resolves on your laptop, but fails only inside Docker or SailThe container cannot reach a DNS serverFix - 3
getent prints nothing for the host, even on the plain serverThe host name does not exist from where the code runsFix
- 1
What does the error look like?
cURL error 6: Could not resolve host: api.example.com (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)Illuminate\Http\Client\ConnectionException: cURL error 6: Could not resolve hostGuzzleHttp\Exception\ConnectException: cURL error 6
Cause 1 of 3
The host in the URL is empty or wrong because env() returns null
1Check
Look at where the base URL comes from. If the code calls env('API_URL') outside a config file, the value becomes null once the configuration is cached.
grep -rn "env('API" app/ routes/
php artisan config:show services2Fix
Read the value in a config file (for example config/services.php) and use config('services.api.url') in your code. Then rebuild the cache.
// config/services.php
'api' => ['url' => env('API_URL')],
// in your code
Http::baseUrl(config('services.api.url'))->get('/health');
php artisan config:cache3Verify
Run the call again and confirm the URL printed by debug has the full host.
php artisan tinker --execute="echo config('services.api.url');"Confirmed by laravel.com
Cause 2 of 3
The container cannot reach a DNS server
1Check
Ask the container itself to look the host up. A container on the default bridge network gets a copy of the host's /etc/resolv.conf; a container on your own network uses Docker's built-in DNS at 127.0.0.11.
docker compose exec app cat /etc/resolv.conf
docker compose exec app getent hosts api.example.com2Fix
Fix DNS on the Docker host first, because containers use the host's DNS servers by default. If you must, override it per container with --dns.
docker run --dns 1.1.1.1 --dns 8.8.8.8 my-image3Verify
Start the container again and repeat the lookup.
docker run --rm --dns 1.1.1.1 my-image getent hosts api.example.comConfirmed by docs.docker.com
Cause 3 of 3
The host name does not exist from where the code runs
1Check
Error 6 is libcurl's code for a host it could not resolve. Test the same name outside PHP, from the same machine.
getent hosts api.example.com
curl -sS -o /dev/null -w "%{http_code}\n" https://api.example.com/2Fix
Correct the spelling or the environment (a staging host used on a machine that is not on the private network). If the name is right and public, the DNS record or your server's resolver is the thing to fix.
# set the correct host in .env, then
php artisan config:cache3Verify
curl reaches the host and returns an HTTP status.
curl -sS -I https://api.example.com/Confirmed by curl.se
How do I stop it coming back?
- Call env() only inside config files; use config() everywhere else, so cached configuration cannot blank a URL.
- Keep API base URLs in config/services.php with one clear key per service.
- Use the Http client's debug option when you add a new integration, to see the real URL once.
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:
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
- EZ EstimaterConstruction quoting and job tracking for contractors, on a Laravel and Firebase stack.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
Related fixes
Back end
Setting a longer (or shorter) timeout for Laravel's Http client
Request takes 30 seconds, then fails
The Laravel Http client waits 30 seconds for a response and 10 seconds to connect.
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
php artisan serve starts but the app is not reachable, or the .env is ignored
Server stops right after start with a non-zero exit code when --port is given and the port is busy
php artisan serve listens on 127.0.0.1 port 8000 by default.
4 causes, each with a fixBack end
Laravel cannot find the database host: getaddrinfo failed
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed
The message means PHP could not turn DB_HOST into an IP address.
3 causes, each with a fixBack 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 fix