Good morning. I am creating a script that queries an API. As the data inside the API became larger the requests needed to be split then for a query sometimes I have to query the API 4 or 5 times. I got in touch with the API support I use and was informed to do parallel queries using Thread.
I do not have much knowledge in programming and I'm using a half-ready code that I found on the internet. But I do not know how to modify it. Follow below:
<?php
// Classe que aguarda um tempo aleatorio e depois imprime algo na tela
class AguardaRand extends Thread {
// ID da thread (usado para identificar a ordem que as threads terminaram)
protected $id;
// Construtor que apenas atribui um ID para identificar a thread
public function __construct($id) {
$this->id = $id;
}
// Metodo principal da thread, que sera acionado quando chamarmos "start"
public function run() {
// Sortear um numero entre 1 e 4
$tempo_rand = mt_rand(1, 4);
// Aguardar o tempo sorteado
sleep($tempo_rand);
// Imprimir quem e' a thread e quanto tempo ela aguardou
printf(
"Sou a thread %d e aguardei %d segundos\n",
$this->id,
$tempo_rand
);
}
}
/// Execucao do codigo
// Criar um vetor com 10 threads do mesmo tipo
$vetor = array();
for ($id = 0; $id < 10; $id++) {
$vetor[] = new AguardaRand($id);
}
// Iniciar a execucao das threads
foreach ($vetor as $thread) {
$thread->start();
}
// Encerrar o script
exit(0);
I need to include a query in CURL
for API
and on each query I need to write the result to a variable can be an Array $ response [] so that I then process the grouped information.
Could anyone help me? I do not need the code. Just explaining to me the logic of this operation already helps me because I can get the script that queries the API unitariamente today and insert it inside.