0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CURLMOPT_MAX_TOTAL_CONNECTIONS curl_multi_exec の使い方

Last updated at Posted at 2023-01-29
<?php

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

    // 同時にオープンする接続の最大数を指定します
    curl_multi_setopt($mh, CURLMOPT_MAX_TOTAL_CONNECTIONS, 6);

    $chs = array_map(function ($url) use ($mh) {
        // cURLリソースを作成します
        $ch = curl_init($url);
        // その他適切なオプションを設定します
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // ハンドルを追加します
        curl_multi_add_handle($mh, $ch);
        return $ch;
    }, $urls);

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

    $contents = array_map(function ($ch) use ($mh) {
        curl_multi_remove_handle($mh, $ch);
        return curl_multi_getcontent($ch);
    }, $chs);

    // ハンドルを閉じます
    curl_multi_close($mh);

    return $contents;
}

$contents = curl_multi(['http://httpbin.org/delay/1', 'http://httpbin.org/delay/1']);
var_dump($contents);

以下のサンプルプログラムを統廃合しました

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?