Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

296
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda