How to change `curl_multi_init` single thread to multi thread

Ask experts how to change to curl_multi_init multi-threaded?
My idea is to split the $data array into array_chunk($data, 15, true); and then do CURL, but I’m a novice, I don’t understand the examples on the Internet, please help

Simulate 100 pieces of data:

function post($url, $data = '', $head = 'application/x-www-form-urlencoded')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:{$head};charset=utf-8;"));
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($ch, CURLOPT_HEADER, 0);
    if (!empty($data)) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

$url = 'http://localhost/test.php';
$res = [];

for ($i=0; $i < 100; $i++) { 
    $res[$key] = post($url, 'key='.$i);
}
var_dump($res);

The code below, I know it must be wrong, please correct me, thank you!!

$newData = array_chunk($data, 10, true);
foreach ($newData as $k=> $tmp) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:application/x-www-form-urlencoded;charset=utf-8;"));
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($ch, CURLOPT_HEADER, 0);

    $mh = curl_multi_init();
    foreach ($tmp as $key => $value) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $tmp);
        curl_multi_add_handle($mh, $ch);
    }
    $active = null;
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    while ($active && $mrc == CURLM_OK) {
        if (curl_multi_select($mh) != -1) {
            do {
                $mrc = curl_multi_exec($mh, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }
    $res[$k] = curl_multi_getcontent($ch);
    curl_multi_remove_handle($mh, $ch);
    curl_multi_close($mh);
}

标签:
php-analytics-data

答案(无需填写):
There were several issues in your original code, which were fixed in the code above.

  1. Each $ch should be an independent handle, not a common one
  2. curl_multi_getcontent should be called on each $ch handle, you are equivalent to only calling the last $ch of each group.
  3. curl_multi_remove_handle should be called on each $ch handle. It is more appropriate to call it after the previous curl_multi_getcontent.
$newData = array_chunk($data, 10, true);
foreach ($newData as $k => $tmp) {
    $mh = curl_multi_init();
    $chs = [];
    foreach ($tmp as $key => $url) {
        $ch = curl_init();
        $chs[$key] = $ch;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/x-www-form-urlencoded;charset=utf-8;'));
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $tmp);
        curl_multi_add_handle($mh, $ch);
    }
    $active = null;
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    while ($active && $mrc == CURLM_OK) {
        if (curl_multi_select($mh) != -1) {
            do {
                $mrc = curl_multi_exec($mh, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }
    foreach ($chs as $key => $ch) {
        $res[$k][$key] = curl_multi_getcontent($ch);
        curl_multi_remove_handle($mh, $ch);
    }

    curl_multi_close($mh);
}


var_dump($res);