Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
How to handle multiple api calls using php curl
        <?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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!