array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'function.curl-multi-exec.php', 1 => 'curl_multi_exec', 2 => 'Run the sub-connections of the current cURL handle', ), 'up' => array ( 0 => 'ref.curl.php', 1 => 'Функції cURL', ), 'prev' => array ( 0 => 'function.curl-multi-errno.php', 1 => 'curl_multi_errno', ), 'next' => array ( 0 => 'function.curl-multi-getcontent.php', 1 => 'curl_multi_getcontent', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/curl/functions/curl-multi-exec.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>
(PHP 5, PHP 7, PHP 8)
curl_multi_exec — Run the sub-connections of the current cURL handle
Processes each of the handles in the stack. This method can be called whether or not a handle needs to read or write data.
mhМультидескриптор cURL, якого повертає curl_multi_init().
still_runningA reference to a flag to tell whether the operations are still running.
A cURL code defined in the cURL Predefined Constants.
Зауваження:
This only returns errors regarding the whole multi stack. There might still have occurred problems on individual transfers even when this function returns
CURLM_OK.
| Версія | Опис |
|---|---|
| 8.0.0 |
multi_handle тепер має бути примірником класу
CurlMultiHandle; раніше очікувався
resource.
|
Приклад #1 curl_multi_exec() example
This example will create curl handles for a list of URLs, add them to a multi handle, and process them asynchronously.
<?php
$urls = [
"https://www.php.net/",
"https://www.example.com/",
];
$mh = curl_multi_init();
$map = new WeakMap();
foreach ($urls as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh, $ch);
$map[$ch] = $url;
}
do {
$status = curl_multi_exec($mh, $unfinishedHandles);
if ($status !== CURLM_OK) {
throw new \Exception(curl_multi_strerror(curl_multi_errno($mh)));
}
while (($info = curl_multi_info_read($mh)) !== false) {
if ($info['msg'] === CURLMSG_DONE) {
$handle = $info['handle'];
curl_multi_remove_handle($mh, $handle);
$url = $map[$handle];
if ($info['result'] === CURLE_OK) {
$statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
echo "Request to {$url} finished with HTTP status {$statusCode}:", PHP_EOL;
echo curl_multi_getcontent($handle);
echo PHP_EOL, PHP_EOL;
} else {
echo "Request to {$url} failed with error: ", PHP_EOL;
echo curl_strerror($info['result']);
echo PHP_EOL, PHP_EOL;
}
}
}
if ($unfinishedHandles) {
if (($updatedHandles = curl_multi_select($mh)) === -1) {
throw new \Exception(curl_multi_strerror(curl_multi_errno($mh)));
}
}
} while ($unfinishedHandles);
curl_multi_close($mh);
?>