swoole是一种php高级web开发框架,可以提升网站的开发效率。本篇文章中小编将介绍如何用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"}
NULL4.结束进程
kill 952
相关教程:
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号