PHP前端开发

如何用curl_multi_init将单线程curl请求改写成多线程并行处理?

百变鹏仔 3天前 #PHP
文章标签 多线程

curl_multi_init 单线程改多线程

问题:

如何将下列单线程 curl 请求改写为使用 curl_multi_init 实现多线程并行处理?

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 = [];//模拟100条数据for ($i=0; $i < 100; $i++) {     $res[$key] = post($url, 'key='.$i);}var_dump($res);

解答:

将数据分组后,使用 curl_multi_init 创建多线程句柄集合,每个分组内的数据处理由独立的句柄负责,并行执行。

$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);