I would like to know how to get the return via the multi curl because by normal curl just use the function curl_exec
to get the result of the page but in the multi curl I can not get anything nor with the curl_exec
.
I would like to know how to get the return via the multi curl because by normal curl just use the function curl_exec
to get the result of the page but in the multi curl I can not get anything nor with the curl_exec
.
First, curl_multi
is not "compatible" with the curl_*
functions. That is, first you create cURL normally (using curl_init()
and curl_setopt()
), then add this cURL handle in curl_multi_add_handle
.
Since you have curl_multi_init
, you should only use curl_multi_*
functions.
To run:
curl_multi_exec()
But, it has a "mystery" because the documentation itself will do the CPU go to 100% , or even enter an infinite loop.
There are two ways to contain this, using:
$executando = 1;
while($executando > 0){
curl_multi_exec($mh, $executando);
curl_multi_select($mh);
}
Another option would be to use usleep
:
$executando = 1;
while($running > 0){
curl_multi_exec($mh, $executando);
usleep(10);
}
Now how do I get the return, since all are within curl_multi_exec
? There are other functions in comparison:
CURL CURL MULTI
curl_exec() curl_multi_exec()
curl_multi_getcontent()
curl_close() curl_multi_remove_handle()
curl_multi_close()
To get curl_getinfo
you can directly use curl_getinfo($ch)
itself, now if you want to get cURL Multi information, use curl_multi_info_read($mh)
instead.
In one usage example, getting content from multiple pages:
// Cria o primeiro cURL
$ch1 = curl_init('https://jsonplaceholder.typicode.com/posts/1');
curl_setopt_array($ch1, [
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => 0, // Isto é inseguro, não use isto em produção, altere para '1'.
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1
]);
// Copia o primeiro cURL e muda a URL, criando um segundo cURL
$ch2 = curl_copy_handle($ch1);
curl_setopt($ch2, CURLOPT_URL, 'https://api.fixer.io/latest');
// Cria o cURL Multi
$mh = curl_multi_init();
// Importa os cURL criados
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
// Executa o cURL Multi
$running = 1;
while($running > 0){
curl_multi_exec($mh, $running);
curl_multi_select($mh);
}
// Exibe primeiro cURL:
echo curl_multi_getcontent($ch1);
// Exibe segundo cURL:
echo curl_multi_getcontent($ch2);
// Fecha o cURL Multi:
curl_multi_close($mh);
// Fecha o cURL:
curl_close($ch1);
curl_close($ch2);
This will return two JSON, distinct, one is from this page and the other this .