发布于2026-07-23 阅读(0)
扫一扫,手机访问
PHP 的同步阻塞模型大家都很熟悉,但一旦涉及并发 HTTP 请求,很多开发者就会头疼——毕竟不像 Go 或 Node.js 那样天生支持异步。不过别急,方案还是有的,而且不止一种。下面就把几种主流方法挨个拆解,看看哪种更适合你的场景。
PHP 自带的 cURL 扩展就提供了多线程能力,核心是 curl_multi_* 系列函数。虽然名字叫“多线程”,但实际是异步 I/O 的并发处理,能同时发起多个请求,然后等待所有响应返回。
示例代码:
$urls = [
'https://example.com/api/1',
'https://example.com/api/2',
'https://example.com/api/3',
];
$mh = curl_multi_init(); // 初始化多线程 cURL
$handles = [];
foreach ($urls as $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $ch); // 将单个 cURL 句柄添加到多线程中
$handles[] = $ch;
}
$running = null;
do {
curl_multi_exec($mh, $running); // 执行并行请求
curl_multi_select($mh); // 等待活动
} while ($running > 0);
$responses = [];
foreach ($handles as $ch) {
$responses[] = curl_multi_getcontent($ch); // 获取每个请求的响应
curl_multi_remove_handle($mh, $ch); // 移除句柄
curl_close($ch);
}
curl_multi_close($mh); // 关闭多线程 cURL
print_r($responses);
优点:
缺点:
Guzzle 是 PHP 生态里最流行的 HTTP 客户端库,没有之一。它内置了异步请求支持,用起来非常顺手。
示例代码:
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$client = new Client();
$urls = [
'https://example.com/api/1',
'https://example.com/api/2',
'https://example.com/api/3',
];
$promises = [];
foreach ($urls as $url) {
$promises[] = $client->getAsync($url); // 发起异步请求
}
$responses = Promise\Utils::settle($promises)->wait(); // 等待所有请求完成
foreach ($responses as $response) {
if ($response['state'] === 'fulfilled') {
echo $response['value']->getBody() . "\n"; // 输出响应内容
} else {
echo 'Request failed: ' . $response['reason']->getMessage() . "\n";
}
}
优点:
缺点:
Swoole 是 PHP 高性能扩展的代表,协程、异步 I/O 一应俱全。用它处理并发 HTTP 请求,性能非常突出。
示例代码:
Swoole\Runtime::enableCoroutine(); // 启用协程
$urls = [
'https://example.com/api/1',
'https://example.com/api/2',
'https://example.com/api/3',
];
$responses = [];
go(function () use ($urls, &$responses) {
$client = new Swoole\Coroutine\Http\Client('example.com', 443, true);
foreach ($urls as $url) {
$client->get($url);
$responses[] = $client->body;
}
});
Swoole\Event::wait(); // 等待所有协程完成
print_r($responses);
优点:
缺点:
ReactPHP 是事件驱动的 PHP 异步框架,核心思想是事件循环,适合处理长连接或流式数据。
示例代码:
require 'vendor/autoload.php';
use React\EventLoop\Factory;
use React\HttpClient\Client;
use React\HttpClient\Response;
$loop = Factory::create();
$client = new Client($loop);
$urls = [
'https://example.com/api/1',
'https://example.com/api/2',
'https://example.com/api/3',
];
foreach ($urls as $url) {
$request = $client->request('GET', $url);
$request->on('response', function (Response $response) {
$response->on('data', function ($chunk) {
echo $chunk;
});
});
$request->end();
}
$loop->run();
优点:
缺点:
如果任务本身是 CPU 密集型的,或者需要真正的并行,可以考虑用 pcntl_fork 创建子进程。每个子进程独立处理一个请求,互不干扰。
示例代码:
$urls = [
'https://example.com/api/1',
'https://example.com/api/2',
'https://example.com/api/3',
];
$children = [];
foreach ($urls as $url) {
$pid = pcntl_fork();
if ($pid == -1) {
die('Could not fork');
} elseif ($pid) {
$children[] = $pid; // 父进程记录子进程 ID
} else {
// 子进程处理请求
echo file_get_contents($url) . "\n";
exit(); // 子进程退出
}
}
// 父进程等待所有子进程完成
foreach ($children as $pid) {
pcntl_waitpid($pid, $status);
}
优点:
缺点:
这么多方法,到底怎么选?一句话总结:
根据你的实际需求 —— 是追求性能、开发效率,还是简单粗暴 —— 选一个最顺手的就好。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8