exssah / exss
这是一个使用 PHP 创建异步 HTTP 服务器的库。
0.1.0
2023-10-09 04:45 UTC
Requires
- react/async: 4.1
- react/event-loop: 1.4
- react/http: 1.9
- react/promise: ^3 || ^2 || ^1
This package is not auto-updated.
Last update: 2024-09-24 08:29:29 UTC
README
Exss 是一个使用 PHP 创建异步 HTTP 服务器的库。它基于 ReactPHP 框架构建。使用这个库,您可以使用 PHP 创建自己的异步 Web 应用程序,并且它与 Express.js 类似。
安装
使用包管理器 Composer 安装 Exss。
composer require exssah/exss
用法
基本 http 服务器
require __DIR__ . '/vendor/autoload.php'; use Exssah\Exss\Exss; use Exssah\Exss\Req; use Exssah\Exss\Res; # create a object of Exss class $app = new Exss(); # use route methods to accept asynchronous HTTP requests $app::get('/hello', function(Req $req, Res $res){ return $res::send('Hello world'); }); # start the server at port 8080 $app::listen(8080, function(){ echo 'https://:8080'; });
接受请求参数
示例: https://:8080/user?id=5&name=Somnath Gupta
require __DIR__ . '/vendor/autoload.php'; use Exssah\Exss\Exss; use Exssah\Exss\Req; use Exssah\Exss\Res; # create a object of Exss class $app = new Exss(); # use route methods to accept asynchronous HTTP requests #ex:- https://:8080/user?id=5&name=Somnath Gupta $app::get('/user', function(Req $req, Res $res){ $userID = $req::params('id'); #'null' if not exist $userName = $req::params('name'); #'null' if not exist #use the sendJson method to send a JSON response return $res::sendJson([ 'id' => $userID, 'name' => $userName, ]); }); # start the server at port 8080 $app::listen(8080, function(){ echo 'https://:8080'; });
接受请求体
URL:- https://:8080/user
请求体
{
"username" : "Somnath Gupta",
"gmail" : "testgmail@gmail.com",
"password" : "U7%)#",
}
require __DIR__ . '/vendor/autoload.php'; use Exssah\Exss\Exss; use Exssah\Exss\Req; use Exssah\Exss\Res; # create a object of Exss class $app = new Exss(); # use route methods to accept asynchronous HTTP requests $app::post('/user', function(Req $req, Res $res){ $username = $req::body('username'); #'null' if not exist $gmail = $req::body('gmail'); #'null' if not exist $password = $req::body('password'); #'null' if not exist #use the sendJson method to send a JSON response return $res::sendJson([ 'username' => $username, 'gmail' => $gmail, 'password' => $password, ]); }); # start the server at port 8080 $app::listen(8080, function(){ echo 'https://:8080'; });
您可以使用 ReactPHP 框架 的其他 fetcher。
1) Promise , 2) Async Utilities, 3) 浏览器 API
贡献
欢迎拉取请求。对于主要更改,请首先打开一个问题以讨论您想要更改的内容。
步骤 1:在您的本地计算机上下载、安装并配置 PHP 和 Composer。
步骤 2:现在 Fork 一个仓库
步骤 3:将代码下载到您的本地机器
步骤 4:打开 'Exss' 文件夹
步骤 5:打开终端
步骤 6:运行此命令以下载所有必要的依赖项。
composer install
步骤 7:如果您不了解 ReactPHP 框架,则可以访问那里。