swoole创建web服务器的方法介绍(代码示例)
来源:不言
发布时间:2019-01-18 14:32:25
阅读量:1247
本篇文章给大家带来的内容是关于swoole创建web服务器的方法介绍(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
http_server.php
1 2 3 4 5 6 7 8 | $http = new swoole_http_server("0.0.0.0", 9501);
$http->on('request', function ($request, $response) {
var_dump($request->get, $request->post);
$response->header('Content-type', 'text/html;charset=utf-8');
$response->end("<h1>Hello Swoole.#" . rand(1000, 9999) . "</h1>\n");
});
$http->start();
|
0.0.0.0 表示监听所有IP地址,一台服务器可能同时有多个IP,如127.0.0.1本地回环IP、192.168.1.100局域网IP、210.127.20.2 外网IP,这里也可以单独指定监听一个IP。
1、启动服务
1 | $ /usr/local/php/bin/php http_server.php
|
2、启动服务成功后,netstat查看
1 2 3 4 | $ ps aux | grep http_server
oosten 952 0.0 2.2 314544 23176 pts/3 Sl+ 14:17 0:00 /usr/local/php/bin/php http_server.php
oosten 953 0.0 0.4 240212 4132 pts/3 S+ 14:17 0:00 /usr/local/php/bin/php http_server.php
oosten 955 0.0 0.7 242620 7408 pts/3 S+ 14:17 0:00 /usr/local/php/bin/php http_server.php
|
3、模拟http请求
1 2 | $ sudo curl
<h1>Hello Swoole.#1061</h1>
|
服务端打印get/post请求数据
1 2 3 4 5 6 | $ /usr/local/php/bin/php http_server.php
array(1) {
["param"]=>
string(1) "1"
}
NULL
|
4、结束进程