skip to Main Content

I have the Http request in my controller:

    $url = url('/api/list-professional');

    $professionals = Http::get($url);

These two lines work perfectly fine when I use them in my phpUnit Tests,
But when I use them inside a controller it just gets a time out.

Anyone knows why?

2

Answers


  1. Wrap your Http::get call in a try-catch block to capture and log any possible exceptions.

    try {
        $professionals = Http::get($url);
    } catch (Exception $e) {
        // Log the exception
        Log::error($e->getMessage());
    }
    

    You can also increase the timeout.

    $professionals = Http::timeout(60)->get($url);
    
    Login or Signup to reply.
  2. The reason for this is that php artisan serve is a single-thread application. So when we use an HTTP client to request from it to itself, it basically just tries to finish the guzzle HTTP client (as a client) first then comes to finish that request (as a server), which is impossible.

    So create two ports using php artisan serve . One is for HTTP client and another port is to access from browser

    for example if you have created port 8000 and 8001 then

    $professionals = Http::get("http://127.0.0.1:8001/api/list-professional");
    

    and use 8000 port to access from browser

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search