http2_client.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. use Swoole\Coroutine as co;
  3. const TEST = array('get', 'post', 'pipeline');
  4. //const TEST = array('pipeline');
  5. //const TEST = array('get',);
  6. CO::set(['trace_flags' => SWOOLE_TRACE_HTTP2,
  7. // 'log_level' => SWOOLE_LOG_TRACE,
  8. ]);
  9. co::create(function () use ($fp)
  10. {
  11. $cli = new co\Http2\Client('127.0.0.1', 9518);
  12. $cli->set([ 'timeout' => 1, 'package_max_length' => 1024*1024*8]);
  13. var_dump($cli->connect());
  14. if (in_array('get', TEST))
  15. {
  16. $req = new Swoole\Http2\Request;
  17. $req->path = "/index.html";
  18. $req->headers = [
  19. 'host' => "localhost",
  20. "user-agent" => 'Chrome/49.0.2587.3',
  21. 'accept' => 'text/html,application/xhtml+xml,application/xml',
  22. 'accept-encoding' => 'gzip',
  23. ];
  24. $req->cookies = ['name' => 'rango', 'email' => '1234@qq.com'];
  25. var_dump($cli->send($req));
  26. $resp = $cli->recv();
  27. var_dump($resp);
  28. }
  29. if (in_array('post', TEST))
  30. {
  31. $req2 = new Swoole\Http2\Request;
  32. $req2->path = "/index.php";
  33. $req2->headers = [
  34. 'host' => "localhost",
  35. "user-agent" => 'Chrome/49.0.2587.3',
  36. 'accept' => 'text/html,application/xhtml+xml,application/xml',
  37. 'accept-encoding' => 'gzip',
  38. ];
  39. $req2->data = "hello world\n";
  40. var_dump($cli->send($req2));
  41. $resp = $cli->recv();
  42. var_dump($resp);
  43. }
  44. if (in_array('pipeline', TEST))
  45. {
  46. $req3 = new Swoole\Http2\Request;
  47. $req3->path = "/index.php";
  48. $req3->headers = [
  49. 'host' => "localhost",
  50. "user-agent" => 'Chrome/49.0.2587.3',
  51. 'accept' => 'text/html,application/xhtml+xml,application/xml',
  52. 'accept-encoding' => 'gzip',
  53. ];
  54. $req3->pipeline = true;
  55. $req3->method = "POST";
  56. $streamId = $cli->send($req3);
  57. $cli->write($streamId, ['int' => rand(1000, 9999)]);
  58. $cli->write($streamId, ['int' => rand(1000, 9999)]);
  59. //end stream
  60. $cli->write($streamId, ['int' => rand(1000, 9999), 'end' => true], true);
  61. var_dump($cli->recv());
  62. }
  63. // $dpcli->close();
  64. });