设为首页 加入收藏

TOP

swoole 创建web服务器
2019-08-24 00:02:27 】 浏览:29
Tags:swoole 创建 web 服务器

http_server.php

$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.启动服务

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

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

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

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

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

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

  4.结束进程

kill 952

 

 

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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇swoole 创建UDP服务器 下一篇Thinkphp5 对接百度云对象存储 BO..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目