-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathswoole_http_client.php
51 lines (44 loc) · 1.52 KB
/
swoole_http_client.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
ini_set('display_errors',1);
//error_reporting(E_ALL);
error_reporting(0);
$http = new swoole_http_server("", 9501, SWOOLE_BASE);
$http->set([
//'worker_num' => 2,
]);
$i = 0;
$http->on('request', function ($request, swoole_http_response $response)use(&$i) {
$route = $request->server['request_uri'];
if($route == '/info'){
$response->end(json_encode($request));
return;
}
$cli = new swoole_http_client('127.0.0.1', 9501);
$cli->set([
'timeout' => 0.3,
'keep_alive' => 1,
]);
//post request
$cli->setData(http_build_query(['a'=>123,'b'=>"哈哈"]));
$cli->setHeaders(['User-Agent' => "swoole"]);
$cli->on('close', function($cli)use($response){
// echo "close\n";
});
$cli->on('error', function($cli) use ($response){
$response->end("error");
});
$cli->execute('/info', function($cli)use( $response, &$i){
$cli->setHeaders(['User-Agent' => "swoole"]);
//get request
$cli->execute('/info', function($cli)use($response, &$i){
$ret = json_encode($cli->headers) . "\nSERVER RESPONSE: ". $cli->body;
$response->end($ret);
$cli->close();
});
});
if($i++ == 1000){
echo "----->Mem: ", memory_get_usage(), "b\n";
$i = 0;
}
});
$http->start();