note 48813 added to function.curl-multi-exec
| From: | dtorop933 at gmail dot com | Date: | Fri, 07 Jan 2005 18:21:21 +0000 |
| Subject: | note 48813 added to function.curl-multi-exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-82915@lists.php.net to get a copy of this message | ||
Here is a revision of rhondle's example. It uses curl_multi_select() to reduce CPU usage while
waiting on the network. It is also includes some error handling and cleanup code.
<?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);
?>
----
Manual Page -- http://www.php.net/manual/en/function.curl-multi-exec.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48813
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48813&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48813&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48813&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48813&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48813&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48813&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48813&report=yes
Search -- http://master.php.net/manage/user-notes.php