PHP file_get_contents(): failed to open stream: Connection refused
Warning: file_get_contents(http://localhost:8000/api): Failed to open stream: Connection refusedShort answer
Connection refused means PHP reached the machine but nothing accepted the connection on that port. Test the same URL with curl from the same host first. Then check three things: the target service is not running or uses another port, localhost inside a container is the container itself, or the network requires an HTTP proxy.
Fix it fast
1Find out which cause you have
Make the same request from the same machine or container with curl. If curl is refused too, PHP is not at fault.
curl -v http://localhost:8000/api2Match what you see, then jump to its fix
- 1
curl also says Failed to connect ... Connection refusedNothing listens on that host and portFix - 2
The service works from your host browser but not from the PHP containerThe PHP code runs in a Docker container and localhost points at the containerFix - 3
curl works only with --proxy, direct requests to the internet are refusedThe network only allows outbound traffic through a proxyFix
- 1
What does the error look like?
Warning: file_get_contents(http://localhost:8000/api): Failed to open stream: Connection refused
Cause 1 of 3
Nothing listens on that host and port
1Check
libcurl describes this class of failure as a failed connect() to the host or proxy. List what listens on the port.
ss -ltnp | grep 80002Fix
Start the service, or correct the host and port in the URL. Do not call your own site from inside a request unless the web server has a free worker for it.
$body = file_get_contents('http://127.0.0.1:8000/api');3Verify
curl should now return a response.
curl -I http://127.0.0.1:8000/apiConfirmed by curl.se
Cause 2 of 3
The PHP code runs in a Docker container and localhost points at the container
1Check
Inside a container, localhost is the container's own network. Containers on a user-defined network reach each other by container name, and a port reaches the outside only if it is published.
docker network ls
docker ps --format '{{.Names}} {{.Ports}}'2Fix
Put both containers on one user-defined network and use the service's container name as the host.
docker network create appnet
docker network connect appnet api
docker network connect appnet php
# in PHP: file_get_contents('http://api:8000/api')3Verify
Run the request from inside the PHP container.
docker exec php curl -I http://api:8000/apiConfirmed by docs.docker.com
Cause 3 of 3
The network only allows outbound traffic through a proxy
1Check
PHP does not use a proxy unless you pass one in the stream context.
env | grep -i proxy2Fix
Set the proxy in the http stream context. Some proxies also need request_fulluri.
$ctx = stream_context_create(['http' => [
'proxy' => 'tcp://proxy.example.com:3128',
'request_fulluri' => true,
]]);
$body = file_get_contents('http://example.com/', false, $ctx);3Verify
Run the script again.
php fetch.phpConfirmed by php.net
How do I stop it coming back?
- Keep service URLs in environment variables, one per environment.
- Check the HTTP result, not only the return value, because file_get_contents returns false on failure.
- Use container names, not localhost, between containers.
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Back 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
docker build stops at RUN composer install with exit code 1
Allowed memory size of ... bytes exhausted
Exit code 1 only says the RUN step failed.
4 causes, each with a fixBack end
memory_limit does not change inside a PHP Docker container
memory_limit is still 128M after editing php.ini in the container
Your setting is being read from the wrong place or overridden.
4 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
Guzzle or Http::get fails with cURL error 6 in Laravel
Illuminate\Http\Client\ConnectionException: cURL error 6: Could not resolve host
cURL error 6 means the machine running PHP could not turn the host name in the URL into an IP address.
3 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 fix