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