csp.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. $serv = new \swoole_http_server("127.0.0.1", 9503, SWOOLE_BASE);
  3. $serv->on('request', function ($req, $resp) {
  4. $chan = new chan(2);
  5. go(function () use ($chan) {
  6. $cli = new Swoole\Coroutine\Http\Client('www.baidu.com', 443, true);
  7. $cli->set(['timeout' => 10]);
  8. $cli->setHeaders([
  9. 'Host' => "www.baidu.com",
  10. "User-Agent" => 'Chrome/49.0.2587.3',
  11. 'Accept' => 'text/html,application/xhtml+xml,application/xml',
  12. 'Accept-Encoding' => 'gzip',
  13. ]);
  14. $ret = $cli->get('/');
  15. $chan->push(['www.baidu.com' => substr(trim(strip_tags($cli->body)), 0, 100)]);
  16. });
  17. go(function () use ($chan) {
  18. $cli = new Swoole\Coroutine\Http\Client('www.taobao.com', 443, true);
  19. $cli->set(['timeout' => 10]);
  20. $cli->setHeaders([
  21. 'Host' => "www.taobao.com",
  22. "User-Agent" => 'Chrome/49.0.2587.3',
  23. 'Accept' => 'text/html,application/xhtml+xml,application/xml',
  24. 'Accept-Encoding' => 'gzip',
  25. ]);
  26. $ret = $cli->get('/');
  27. $chan->push(['www.taobao.com' => substr(trim(strip_tags($cli->body)), 0, 100)]);
  28. });
  29. $result = [];
  30. for ($i = 0; $i < 2; $i++)
  31. {
  32. $result += $chan->pop();
  33. }
  34. $resp->header('Content-Type', 'text/html;charset=utf-8');
  35. $resp->end(var_export($result, true));
  36. });
  37. $serv->start();