
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);以上就是如何用curl_multi_init将单线程curl请求改写成多线程并行处理?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号