How do you send 3 Curl requests at once?

2

I'm trying to create a system, but I have a question, I wanted to send the request all at once through curl_init, I want to send it to fetch 3 servers at a time.

    
asked by anonymous 20.05.2016 / 19:23

2 answers

0

If you have to get 3 servers then there will be 3 requests, but one way is to create a function that takes care of 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')
    
20.05.2016 / 19:33
0

Well this was my code, I'm new to 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);
    
21.05.2016 / 06:35