swoole 建立web服务器

http_server.phpphp

$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。html

  1.启动服务服务器

$ /usr/local/php/bin/php http_server.php 

  2.启动服务成功后,netstat查看swoole

$ 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请求curl

$ sudo curl http://127.0.0.1:9501?param=1
<h1>Hello Swoole.#1061</h1>

  服务端打印get/post请求数据post

$ /usr/local/php/bin/php http_server.php 
array(1) { ["param"]=> string(1) "1" } NULL

  4.结束进程url

kill 952

 

 

参考:https://wiki.swoole.com/wikispa

相关文章
相关标签/搜索