curl_multi_add_handle

(PHP 5, PHP 7, PHP 8)

curl_multi_add_handlecURL マルチハンドルに、通常の 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_XXX のいずれかを返します。

変更履歴

バージョン 説明
8.0.0 multi_handleCurlMultiHandle クラスのインスタンスを期待するようになりました。 これより前のバージョンでは、resource を期待していました。
8.0.0 handleCurlHandle クラスのインスタンスを期待するようになりました。 これより前のバージョンでは、resource を期待していました。

例1 curl_multi_add_handle() の例

この例は、ふたつの cURL ハンドルを作成し、それをマルチハンドルに追加して非同期で実行します。

<?php
// cURL リソースを作成します
$ch1 = curl_init();
$ch2 = curl_init();

// URL およびその他適切なオプションを設定します。
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);

// マルチ cURL ハンドルを作成します
$mh = curl_multi_init();

// ふたつのハンドルを追加します
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

// ハンドルを実行します
do {
$status = curl_multi_exec($mh, $active);
if (
$active) {
curl_multi_select($mh);
}
} while (
$active && $status == CURLM_OK);

// すべてのハンドルを閉じます
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>

参考

add a note add a note

User Contributed Notes 3 notes

up
7
Anonymous
11 years ago
The 'do' loop in this example will cycle indefinitely until all curl downloads complete, needlessly using CPU cycles.  A much better way is to block while dowloads are in progress.  Refer to the example code on the curl_multi_exec() page instead, which does this!
up
2
3315954155 at libero dot it
5 years ago
Sometime with arrays and loops (i.e. for or do while or foreach) curl_multi_init  loses some hits, to work around this problem  it is necessary to divide the loop into two parts: the start with key number zero and than, all the rest. In this way the curl_multi_init doesn't lose any hits. I did a lot of test and it seems to work well enough.
<?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('http://php.net/manual/it/function.curl-multi-add-handle.php',
'http://php.net/manual/en/function.curl-multi-init.php',
'http://php.net/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;
}

?>
up
-3
kamsamlt888 at gmail dot com
5 years ago
$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