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多线程的呢?
我的思路是把$data数组拆分array_chunk($data, 15, true);然后再进行CURL,但我是新手,没有看懂网上的例子,请求帮助
下边的写法,我知道肯定不对,麻烦给位加以指正,谢谢

$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);
}
阅读 2k
2 个回答

改写方案如下

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

你原来的代码中存在几个问题,在上面的代码中被修正。

1、$ch 每一个都应该是独立的句柄,而不是通用一个
2、curl_multi_getcontent 应该调用在每个 $ch 句柄上,你这里相当于只调用每组最后一个 $ch 的。
3、curl_multi_remove_handle 应该调用在每个 $ch 句柄上,这里比较合适的就是在前面的 curl_multi_getcontent 之后调用。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题