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
In Postman and in the browser the api is working
How to call api request from Webcontroller to ApiController in the same project with routes Web.php and Api.php
2
Answers
It’s because you use PHP internal server.
Run another server
php artisan serve --port=7000
and then set the URL tohttp://127.0.0.1:7000/api/v1/profile
Since you are in the same server you could call for the function from the
Webcontroller
like in below