在 PHP 中,由于其传统的同步阻塞模型,实现并行异步处理 HTTP 请求并不像其他语言那样直接,但也可以通过一些扩展和工具来实现,下面小编就来讲讲具体实现方法吧
+
目录
在 PHP 中,由于其传统的同步阻塞模型,实现并行异步处理 HTTP 请求并不像其他语言(如 Go 或 Node.js)那样直接。不过,仍然可以通过一些扩展和工具来实现并行异步处理。以下是几种常见的方法:
1. 使用 cURL 的多线程功能
PHP 的 cURL 扩展支持多线程处理,可以通过 curl_multi_* 系列函数实现并行 HTTP 请求。
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | $urls = [
'https://example.com/api/1' ,
'https://example.com/api/2' ,
'https://example.com/api/3' ,
];
$mh = curl_multi_init();
$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 );
$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 );
print_r( $responses );
|
优点:
- 原生支持,无需额外扩展。
- 可以并行处理多个 HTTP 请求。
缺点:
2. 使用 Guzzle 异步客户端
Guzzle 是一个流行的 PHP HTTP 客户端库,支持异步请求。
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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" ;
}
}
|
优点:
缺点:
需要安装 Guzzle 库。
3. 使用 Swoole 扩展
Swoole 是一个高性能的 PHP 扩展,支持异步、协程和并行处理。
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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 );
|
优点:
缺点:
4. 使用 ReactPHP
ReactPHP 是一个基于事件驱动的 PHP 库,支持异步编程。
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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();
|
优点:
- 基于事件驱动,适合异步编程。
- 支持长连接和流式处理。
缺点:
5. 使用多进程(pcntl 扩展)
PHP 的 pcntl 扩展支持多进程编程,可以通过创建子进程来实现并行处理。
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $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 ;
} else {
echo file_get_contents ( $url ) . "\n" ;
exit ();
}
}
foreach ( $children as $pid ) {
pcntl_waitpid( $pid , $status );
}
|
优点:
缺点:
总结
cURL 多线程:适合简单的并行 HTTP 请求。
Guzzle:代码简洁,适合大多数场景。
Swoole:高性能,适合高并发场景。
ReactPHP:基于事件驱动,适合异步编程。
多进程:适合 CPU 密集型任务,但复杂度较高。
根据具体需求选择合适的方法即可。