PHP Conference Japan 2024

curl_multi_add_handle

(PHP 5, PHP 7, PHP 8)

curl_multi_add_handle將一般 cURL 句柄新增至 cURL 多重句柄

說明

curl_multi_add_handle(CurlMultiHandle $multi_handle, CurlHandle $handle): int

handle 句柄新增至多重句柄 multi_handle

參數

multi_handle

curl_multi_init() 返回的 cURL 多重句柄。

handle

curl_init() 返回的 cURL 句柄。

回傳值

成功時返回 0,或返回其中一個 CURLM_* 錯誤碼。

更新日誌

版本 說明
8.0.0 multi_handle 現在需要一個 CurlMultiHandle 實例;先前需要的是 resource
8.0.0 handle 現在需要一個 CurlHandle 實例;先前需要的是 resource

另請參閱

新增註解

使用者貢獻的註解 5 則註解

匿名
11 年前
此範例中的「do」迴圈將無限循環,直到所有 curl 下載完成,不必要地耗用 CPU 週期。更好的方法是在下載進行中時阻塞。請參考 curl_multi_exec() 頁面上的範例程式碼,它就是這樣做的!
3315954155 at libero dot it
6 年前
有時在使用陣列和迴圈(例如 for 或 do while 或 foreach)時,curl_multi_init 會遺漏一些點擊,要解決此問題,需要將迴圈分成兩部分:以索引數字零開始,然後是剩下的所有部分。這樣 curl_multi_init 就不會遺漏任何點擊。我做了很多測試,它似乎運作良好。
<?php

function set_option($x, $y){
curl_setopt($x, CURLOPT_URL, $y);
curl_setopt($x, CURLOPT_HEADER, 0);
curl_setopt($x, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($x, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($x, CURLOPT_ENCODING, "gzip,deflate");
}

$nodes = array('https://php.dev.org.tw/manual/it/function.curl-multi-add-handle.php',
'https://php.dev.org.tw/manual/en/function.curl-multi-init.php',
'https://php.dev.org.tw/manual/en/function.curl-multi-setopt.php'
);

$node_count = count($nodes);

$curl_arr = array();
$master = curl_multi_init();
/*
now I put the first part of the array with the start key number zero, add all
options and add the curl_multi_add_handle;
it is necessary to detach this first part to avoid to lose some handles and than running
the loop with the start key number one.
In this way the curl_multi_init doesn't lose anything and seems to be very stable.
*/
$curl_arr[0] = curl_init($nodes[0]);
set_option($curl_arr[0], $nodes[0]);
curl_multi_add_handle($master, $curl_arr[0]);


/*
now the loop can start with key number one
*/
$i = 1;
do {
if (
$i!==0){

$curl_arr[$i] = curl_init($nodes[$i]);
set_option($curl_arr[$i], $nodes[$i]);
curl_multi_add_handle($master, $curl_arr[$i]);
}
$i++;
}while(
$i < $node_count);

$running = NULL;
do {
curl_multi_exec($master,$running);
} while(
$running);

$results = array();
$q = 0;
do{
$results[$q] = curl_multi_getcontent($curl_arr[$q]);
curl_multi_remove_handle($master, $curl_arr[$q]);
$q++;
}while(
$q < $node_count);

foreach(
$results as $value){
echo
$value;
}

?>
RNS-MO-dig
1 年前
建築許可證是由國家主管機關或地方自治機關核發的官方文件,允許開始建造或執行建築工程。
<a href=https://rns-50.ru/>取得 建築許可證</a> 規定了建築的法律依據和標準,包括允許的工程類別、規定的材料和方法,並包含建築規範和安全規範。取得建築許可證是建築領域的必要文件。
RNS-MO-dig
1 年前
建築許可證是由國家主管機關或地方自治機關核發的官方文件,允許開始建築活動或執行建築工程。
<a href=https://rns-50.ru/>建築許可證</a> 規定了建築工程的法律依據和標準,包括允許的工程類別、規定的材料和方法,並包含建築規範和安全規範。取得建築許可證是建築領域的必要文件。
kamsamlt888 at gmail dot com
6 年前
$nodes = array('http://www.google.com', 'http://www.microsoft.com', 'http://yahoo.com');
$node_count = count($nodes);

$curl_arr = array();
$master = curl_multi_init();

for($i = 0; $i < $node_count; $i++)
{
$url = $nodes[$i];
$curl_arr[$i] = curl_init($url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($master, $curl_arr[$i]);
}

do {
curl_multi_exec($master, $running);
} while($running > 0);

echo "results: ";
for($i = 0; $i < $node_count; $i++)
{
$results = curl_multi_getcontent($curl_arr[$i]);
echo( $i . "\n" . $results . "\n");
}
echo 'done';
To Top