I am working on a laravel project where I need to show a list and stats from API. For stats and a list of content, there are separate URLs. And I have to display both on one page.
How to call multiple API URLs on a page?
Suppose https://localhost/api/games and https://localhost/api/stats are two URLs.
I am using Laravel version 7
PHP version 7.2.3
Controller here:
public function index()
{
$collection = Http::withHeaders([
'x-api-host' => $host,
'x-api-key' => $key
])->get('https://localhost/api/games');
return view('welcome', ['collection'=>$collection]);
}
2
Answers
Synchronous call
you can call the second api after the first
in this situation the second http request will be send after your first request has been resolved to a response
Asynchronous call
since maybe you are looking for asynchronous call ( sending http request calls after each other – before the first one response.
Laravel is not a good choice for doing this.
however you can achieve this with below scenario:
gamesCallJob
&statCallJob
)gameCallREsolveEvent
&statCallREsolveEvent
)As esel said Just do that a second time :