如何详细配置并使用PHP Swoole HTTP服务器?
- 内容介绍
- 文章标签
- 相关推荐
本文共计665个文字,预计阅读时间需要3分钟。
原文示例:本文实例讲述了PHP+swoole中http_server的配置与使用方法。分享给广大读者,供大家参考,具体如下:+swoole为我们提供了一个swoole_http_server类,方便我们处理http请求。但它对http协议的支持并不全面。
改写后的内容:本文以实例展示如何在PHP+swoole中使用http_server进行配置和操作。以下内容供大家参考:swoole提供了swoole_http_server类,便于我们处理HTTP请求。然而,其对HTTP协议的支持尚不完善。
本文实例讲述了PHP swoole中img.558idc.com/uploadfile/', ]); //设置request事件回调 //函数有两个参数: //swoole_input'); var_dump($request->rawContent()); //获取完整img.558idc.com/uploadfile/', 'document_root' => __DIR__ . '/statics/', 'enable_static_handler' => true, ]); $server->on('request', function ($request, $response) use ($server) { if ($request->server['path_info'] == '/upload') { $tmp = $request->files['upload']['tmp_name']; $upload = $server->setting['upload_dir'] . $request->files['upload']['name']; if (file_exists($tmp) && move_uploaded_file($tmp, $upload)) { $response->header('Content-Type', 'text/html; charset=UTF-8'); $response->end('上传成功'); } else { $response->end('上传失败'); } } }); //启动服务 $server->start();
我们在statics目录下创建一个upload.html文件:
<!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>文件上传</title> </head> <body> <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="upload" value=""> <input type="submit" value="提交"> </form> </body> </html>
四、处理路由文件自动加载
<?php //创建一个http server服务 $server = new swoole_http_server('0.0.0.0', 8888); $server->set([ //配置项目的目录 'project_path' => __DIR__ . '/src/', ]); $server->on('WorkerStart', function ($server, $worker_id) { //注册自动加载函数 spl_autoload_register(function ($class) use($server) { $class = $server->setting['project_path'] . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php'; if (file_exists($class)) { include_once $class; } }); }); $server->on('request', function ($request, $response) use ($server) { $pathInfo = explode('/', ltrim($request->server['path_info'], '/')); //模块/控制器/方法 $module = $pathInfo[0] ?? 'Index'; $controller = $pathInfo[1] ?? 'Index'; $method = $pathInfo[2] ?? 'index'; try { $class = "\\{$module}\\{$controller}"; $result = (new $class)->{$method}(); $response->end($result); } catch (\Throwable $e) { $response->end($e->getMessage()); } }); //启动服务 $server->start();
我们在目录 src 下创建 test 目录,并创建 test.php 文件
<?php namespace Test; class Test { public function test() { return 'test'; } }
然后访问 127.0.0.1:8888/test/test/test 就可以看到返回结果了。
通过$request->server['path_info'] 来找到模块,控制器,方法,然后注册我们自已的加载函数,引入文件。实例化类对象,然后调用方法,返回结果。
注意,不要将 spl_autoload_register 放到 onStart 事件回调函数中。
onStart 回调中,仅允许echo、打印Log、修改进程名称。不得执行其他操作。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP网络编程技巧总结》、《php socket用法总结》、《php面向对象程序设计入门教程》、《PHP数据结构与算法教程》及《php程序设计算法总结》
希望本文所述对大家PHP程序设计有所帮助。
本文共计665个文字,预计阅读时间需要3分钟。
原文示例:本文实例讲述了PHP+swoole中http_server的配置与使用方法。分享给广大读者,供大家参考,具体如下:+swoole为我们提供了一个swoole_http_server类,方便我们处理http请求。但它对http协议的支持并不全面。
改写后的内容:本文以实例展示如何在PHP+swoole中使用http_server进行配置和操作。以下内容供大家参考:swoole提供了swoole_http_server类,便于我们处理HTTP请求。然而,其对HTTP协议的支持尚不完善。
本文实例讲述了PHP swoole中img.558idc.com/uploadfile/', ]); //设置request事件回调 //函数有两个参数: //swoole_input'); var_dump($request->rawContent()); //获取完整img.558idc.com/uploadfile/', 'document_root' => __DIR__ . '/statics/', 'enable_static_handler' => true, ]); $server->on('request', function ($request, $response) use ($server) { if ($request->server['path_info'] == '/upload') { $tmp = $request->files['upload']['tmp_name']; $upload = $server->setting['upload_dir'] . $request->files['upload']['name']; if (file_exists($tmp) && move_uploaded_file($tmp, $upload)) { $response->header('Content-Type', 'text/html; charset=UTF-8'); $response->end('上传成功'); } else { $response->end('上传失败'); } } }); //启动服务 $server->start();
我们在statics目录下创建一个upload.html文件:
<!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>文件上传</title> </head> <body> <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="upload" value=""> <input type="submit" value="提交"> </form> </body> </html>
四、处理路由文件自动加载
<?php //创建一个http server服务 $server = new swoole_http_server('0.0.0.0', 8888); $server->set([ //配置项目的目录 'project_path' => __DIR__ . '/src/', ]); $server->on('WorkerStart', function ($server, $worker_id) { //注册自动加载函数 spl_autoload_register(function ($class) use($server) { $class = $server->setting['project_path'] . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php'; if (file_exists($class)) { include_once $class; } }); }); $server->on('request', function ($request, $response) use ($server) { $pathInfo = explode('/', ltrim($request->server['path_info'], '/')); //模块/控制器/方法 $module = $pathInfo[0] ?? 'Index'; $controller = $pathInfo[1] ?? 'Index'; $method = $pathInfo[2] ?? 'index'; try { $class = "\\{$module}\\{$controller}"; $result = (new $class)->{$method}(); $response->end($result); } catch (\Throwable $e) { $response->end($e->getMessage()); } }); //启动服务 $server->start();
我们在目录 src 下创建 test 目录,并创建 test.php 文件
<?php namespace Test; class Test { public function test() { return 'test'; } }
然后访问 127.0.0.1:8888/test/test/test 就可以看到返回结果了。
通过$request->server['path_info'] 来找到模块,控制器,方法,然后注册我们自已的加载函数,引入文件。实例化类对象,然后调用方法,返回结果。
注意,不要将 spl_autoload_register 放到 onStart 事件回调函数中。
onStart 回调中,仅允许echo、打印Log、修改进程名称。不得执行其他操作。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP网络编程技巧总结》、《php socket用法总结》、《php面向对象程序设计入门教程》、《PHP数据结构与算法教程》及《php程序设计算法总结》
希望本文所述对大家PHP程序设计有所帮助。

