skip to Main Content

I have WebController and ApiController with the routes Web.php and Api.php

I need to send api request from my WebController to ApiController to collect the api response.

In the postman api request and response is working fine. But when i try with the WebController it’s getting cURL timeout.

Below are my codes in WebController and ApiController

In WebController

use IlluminateSupportFacadesHttp;

public function profile()
    {
        $apiURL = 'http://127.0.0.1:8000/api/v1/profile';

        $response = Http::get($apiURL);

        echo $response;
    }

In ApiController

public function profile()
    {
        return response()->json([
                'message'   => 'Success',
            ], 200);
    }

But getting an error in the WebController

broswer response

In Postman and in the browser the api is working

postman img

browser img

How to call api request from Webcontroller to ApiController in the same project with routes Web.php and Api.php

2

Answers


  1. It’s because you use PHP internal server.

    Run another server php artisan serve --port=7000 and then set the URL to http://127.0.0.1:7000/api/v1/profile

    Login or Signup to reply.
  2. Since you are in the same server you could call for the function from the Webcontroller like in below

    return (new ApiController())->profile();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search