Can anybody help me understand why are there two loops here?
<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
//create the multiple cURL handle
$mh = curl_multi_init();
//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$active = null;
//execute the handles
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>
Code is taken from http://php.net/manual/en/function.curl-multi-exec.php – the first example
Another issue is that the code strangely outputs everything to screen which never happened when using curl without the multi_exec feature. I needed to echo the content before, but not for this one, its blurts out everything without even asking.
There is no good explanation for that. It can indeed be moved into a single loop.
I believe this example code originates from demo code we did in plain C in the curl project that made that first call/loop to see if it should go on or not.
You can easily rewrite the code to use just a single loop.