Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : CURL, Client URL Library Functions : curl_multi_exec

curl_multi_exec

Run the sub-connections of the current cURL handle (PHP 5)
int curl_multi_exec ( resource mh, int &still_running )

Example 434. curl_multi_exec() example

This example will create two cURL handles, add them to a multi handle, and then run them in parallel.

<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();

// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://www.example.com/");
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);

$running=null;
//execute the handles
do {
   
$mrc = curl_multi_exec($mh, $running);
} 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 Examples / Notes » curl_multi_exec

substr "iscampifriese",1,11 dot "

If you are using mulit handles and you wish to re-execute any of them (if they timed out or something), then you need to remove the handles from the mulit-handle and then re-add them in order to get them to re-execute.  Otherwise cURL will just give you back the same results again without actually retrying.

robert dot reichel

I was testing PHP code provided by dtorop933@gmail.com in curl_multi_exec section of PHP Manual.
The part of the code '$err = curl_error($conn[$i])' should return error message for each cURL session, but it does not.
The function curl_error() works well with the curl_exec(). Is there any other solution for getting session error message with curl_multi_exec() or there is a bug in cURL library.
The script was tested with Windows XP and PHP-5.2.4


shichuanr

For people who have problem running the above example script(Example 425. curl_multi_exec()), you can alter it a little bit to make it work properly by replacing the existing chunk of code with the one below:
<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://www.google.com/");
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);
$running=null;
//execute the handles
do {
   curl_multi_exec($mh,$running);
} while ($running > 0);
//close the handles
curl_multi_remove_handle($mh,$ch1);
curl_multi_remove_handle($mh,$ch1);
curl_multi_close($mh);
?>


dtorop933

<?php
$connomains = array(
  "http://www.cnn.com/",
  "http://www.canada.com/",
  "http://www.yahoo.com/"
);
$mh = curl_multi_init();
foreach ($connomains as $i => $url) {
 $conn[$i] = curl_init($url);
 curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
 curl_multi_add_handle ($mh,$conn[$i]);
}
// start performing the request
do {
 $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active and $mrc == CURLM_OK) {
 // wait for network
 if (curl_multi_select($mh) != -1) {
   // pull in any new data, or at least handle timeouts
   do {
     $mrc = curl_multi_exec($mh, $active);
   } while ($mrc == CURLM_CALL_MULTI_PERFORM);
 }
}
if ($mrc != CURLM_OK) {
 print "Curl multi read error $mrc\n";
}
// retrieve data
foreach ($connomains as $i => $url) {
 if (($err = curl_error($conn[$i])) == '') {
   $res[$i]=curl_multi_getcontent($conn[$i]);
 } else {
   print "Curl error on handle $i: $err\n";
 }
 curl_multi_remove_handle($mh,$conn[$i]);
 curl_close($conn[$i]);
}
curl_multi_close($mh);
print_r($res);
?>


Change Language


Follow Navioo On Twitter
Constants
curl_close
curl_copy_handle
curl_errno
curl_error
curl_exec
curl_getinfo
curl_init
curl_multi_add_handle
curl_multi_close
curl_multi_exec
curl_multi_getcontent
curl_multi_info_read
curl_multi_init
curl_multi_remove_handle
curl_multi_select
curl_setopt_array
curl_setopt
curl_version
eXTReMe Tracker