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

mdupor's avatar

Curl requests end up in infinite loop

I've inherited a (L5.5) project which when you hit one route makes a Curl request on API endpoint defined in the same project. Without getting in the reasons why it's done and what could I do to make it better, I'd like to know why do those requests end up in an infinite loop and I can't do anything but to kill the process and run artisan serve again.

$url = route($route_name, Arr::get($params, 'route_params', []));

if ($token = Session::get('access_token')) {
    $params['access_token'] = $token;
}
$data = http_build_query($params);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

$result = curl_exec($ch);

curl_close($ch);

$result = json_decode($result, true);

$url resolves to http://127.0.0.1:8000/api/login. When it comes to curl_exec it just gets stuck and it never hits the endpoint controller method.

What could be the cause of this? Any ideas how to debug it?

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

its a bizarre setup, but to answer your question php artisan serve starts php's built in webserver. This has the limitation of only being able to serve a single request at a time.

Its already serving your request so when the app makes a http call to itself, it hangs because it has no available process to serve it

switch to another webserver (apache or nginx), eg with valet

2 likes
mdupor's avatar

Switching to Docker with Nginx server resolved it. Thanks!

Please or to participate in this conversation.