Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

valentin_vranic's avatar

Laravel Sail cUrl

Hi guys. This one makes me crazy for multiple days already.

I'm running my Laravel 10 application with Sail. Within one of my methods I want to curl one api endpoint which is located inside of the app. Route::get('/voipmonitor-stats', [VoipMonitor::class, 'getStats']);

Called from the browser I can reach it, and it works as it supposed to. But when I try to curl from the application, it's not working. The endpoint is in the same container as the app, when I ping the host ip within the container it finds it, even with the container name. But when I curl right now I'm getting this error: cURL error: Empty reply from server and the response is null

This is my curl approach (just for presentation, and testing, otherwise I have a Curl class):

$url = gethostbyname('laravel.app').self::VOIP_MONITOR_API_URL;

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_HEADER, false);

$response = curl_exec($curl);

if (curl_errno($curl)) {
    $error_msg = curl_error($curl);
    echo "Error: ".$error_msg;
} else {
    echo "Response: ".$response;
}

curl_close($curl);
0 likes
2 replies
LaryAI's avatar
Level 58

It seems like you're trying to access a service within the same Docker network using a hostname that might not be recognized by the container's DNS resolver. When you're using Laravel Sail, services are typically accessible to each other by their service names as defined in the docker-compose.yml file.

Here's a solution that might help you:

  1. Make sure that the service name in your docker-compose.yml file is correct. For example, if your Laravel application service is named laravel.test, you should use that name to access it from another service within the same Docker network.

  2. Instead of using gethostbyname('laravel.app'), directly use the service name as the hostname in your cURL request. For example:

$url = 'http://laravel.test' . self::VOIP_MONITOR_API_URL;

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_HEADER, false);

$response = curl_exec($curl);

if (curl_errno($curl)) {
    $error_msg = curl_error($curl);
    echo "Error: " . $error_msg;
} else {
    echo "Response: " . $response;
}

curl_close($curl);
  1. If you're using a non-standard port, make sure to include it in the URL.

  2. Ensure that your application's firewall or security groups (if any) allow for internal communication on the port you're trying to access.

  3. If you're still facing issues, you can try to troubleshoot by executing a cURL command directly from within the container's shell to see if it's a PHP-specific issue or a network-related issue. You can do this by running sail shell to get into the container's command line and then using cURL from there.

Remember that when you're working with Docker containers, you should use the internal network and service names for communication between services, rather than external DNS names or IPs.

Please or to participate in this conversation.