2
        <?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

  1. In these above programs I have been calling three API and getting 3 responses using PHP curl
  2. In the first call it has been taking 2 seconds while waiting for a response and then call next API call
  3. Because of that it's taking more than 6 seconds for getting my data and use them

What do i need

  1. I need to get all 3 responses within 2 seconds
  2. I need to know is there a way to call three API at the same time and get responses at the same time
  3. 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

TBA
  • 1,921
  • 4
  • 13
  • 26
A. Chithrai
  • 121
  • 11
  • 1
    Does this answer your question? [Async curl request in PHP](https://stackoverflow.com/questions/36171222/async-curl-request-in-php) – Jacob Mulquin Oct 22 '21 at 05:14

0 Answers0