<?php
//first api call
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => ' firs api url',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response_userdata = curl_exec($curl);
//first api call response 2 seconds
$data_userdata=json_decode($response_menutable,TRUE);
curl_close($curl);
//second api call
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'seconds api url',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response_stockavldetails = curl_exec($curl);
//second api call response time 2 seconds
$data_stockavldetails=json_decode($response_stockavldetails,TRUE);
curl_close($curl);
//Third api call
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'third api call url',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response_stockoutgoingdetails = curl_exec($curl);
//third api call response time 2 seconds
$data_stockoutgoingdetails=json_decode($response_stockoutgoingdetails,TRUE);
curl_close($curl);
//total response time 6 seconds
?>
What I am doing
- In these above programs I have been calling three API and getting 3 responses using PHP curl
- In the first call it has been taking 2 seconds while waiting for a response and then call next API call
- Because of that it's taking more than 6 seconds for getting my data and use them
What do i need
- I need to get all 3 responses within 2 seconds
- I need to know is there a way to call three API at the same time and get responses at the same time
- or correct way to handle multiple API call and responses to get data faster
Preferable PHP
javascript fetch throwing cors error if calling API in javascript is faster than PHP suggest me that too I will try