How do you send three Curl requests at once?

Asked

Viewed 306 times

2

Good guys I’m creating a system, but I’m having a doubt I wanted to send the request all at once by curl_init, like I want to send to search in 3 servers at once

  • 1

    Welcome to SOPT. Would like to [Edit] your post and add the code you are using, so we can analyze and suggest a change. Thank you.

2 answers

0

Well that was my code, I’m new in PHP

$ch = curl_init('http://meusite.com.br/servidor1.php'); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "id=$id");
$teste = curl_exec ($ch);
curl_close ($ch);

0


If you have to fetch 3 servers then there will have to be 3 requests, but one way to do it is to create a function that takes responsibility for all of them:

function get_data($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, True);
    curl_setopt($curl, CURLOPT_URL, $url);
    $return = curl_exec($curl);
    curl_close($curl);
    return $return;
}

$urls = array('url1', 'url2', 'url3'); // só tem de adicionar/retirar os serviços daqui
$datas = array();
for($i=0; $i<count($urls); $i++) {
   $datas['url_' .$i] = get_data($urls[$i]);
}

// ex: $datas['url_0'] = conteudos do primeiro serviço a que acedeu ('url1')
  • Thank you Miguel for your reply did not work in my code but gave me a light to see how to do thank you even :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.