在 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(); // 初始化多线程 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);

优点:

  • 原生支持,无需额外扩展。
  • 可以并行处理多个 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);

优点:

  • 高性能,支持协程和异步 I/O。
  • 适合高并发场景。

缺点:

  • 需要安装 Swoole 扩展。
  • 学习曲线较高。

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

优点:

  • 基于事件驱动,适合异步编程。
  • 支持长连接和流式处理。

缺点:

  • 需要安装 ReactPHP 库。
  • 代码复杂度较高。

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; // 父进程记录子进程 ID

    } else {

        // 子进程处理请求

        echo file_get_contents($url) . "\n";

        exit(); // 子进程退出

    }

}

// 父进程等待所有子进程完成

foreach ($children as $pid) {

    pcntl_waitpid($pid, $status);

}

优点:

  • 真正的并行处理。
  • 适合 CPU 密集型任务。

缺点:

  • 需要 pcntl 扩展。
  • 进程间通信复杂。

总结

cURL 多线程:适合简单的并行 HTTP 请求。

Guzzle:代码简洁,适合大多数场景。

Swoole:高性能,适合高并发场景。

ReactPHP:基于事件驱动,适合异步编程。

多进程:适合 CPU 密集型任务,但复杂度较高。根据具体需求选择合适的方法即可。

更多推荐