异步Http2.0客户端

4.2.0以后移除了异步Http2客户端, 请使用协程Http2客户端

Swoole-1.9.7增加了对Http2.0客户端的支持。新增的客户端类名为Swoole\Http2\Client,继承自Swoole\Client,实现了Http2.0客户端协议的完整支持。

Http2.0客户端与Http1.1的最大差别是2.0支持了Stream并发机制,可以同时发起多个GETPOST请求。最大并发数量受限与服务器端规定的max_concurrent_streams设置。

编译安装

需要依赖nghttp2库,编译Swoole扩展时需要设置—enable-http2—enable-openssl—with-openssl-dir

使用示例

  1. $array = array(
  2. "host" => "www.jd.com",
  3. "accept-encoding" => "gzip, deflate",
  4. 'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  5. 'accept-language' => 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,ja;q=0.2',
  6. 'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) Chrome/58.0.3026.3 Safari/537.36',
  7. );
  8. $client = new Swoole\Http2\Client("www.jd.com", 443, true);
  9. $client->setHeaders($array);
  10. $client->setCookies(array("a" => "1", "b" => "2"));
  11. $client->get("/", function ($o) use($client) {
  12. echo "#{$client->sock} hello world 1\n";
  13. echo $o->body;
  14. });
  15. $client->post("/", $array, function ($o) use($client) {
  16. echo "{$client->sock} hello world 3\n";
  17. echo $o->body;
  18. $client->close();
  19. });
  20. Swoole\Event::wait();